Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. /* Scaffolding code for Assignment #1: Tic-tac-toe
  2.  
  3. YOU MUST BUILD YOUR IMPLEMENTION BY COMPLETING THE CODE IN THIS TEMPLATE.
  4. YOU ARE NOT ALLOWED TO DELETE ANY LINE OR IGNORE ANY LINE IN THIS CODE.
  5. */
  6.  
  7. /* Your implementation must represent the game's board using a data structure consisting of an array of arrays.
  8. Each nested array represents a row in the board. */
  9. let BOARD = [
  10. [1, 2, 3],
  11. [4, 5, 6],
  12. [7, 8, 9]
  13. ];
  14.  
  15. // While the game runs, the board gets updated with marks placed by the human and computer players using the following values.
  16. const HUMAN_MARK = -1;
  17. const COMPUTER_MARK = 0;
  18.  
  19. /* TASK 5: Complete the play() function.
  20. This function should implement an iteration statement that let both players keep playing until a player wins the game or all positions are filled.
  21.  
  22. PARAMETERS: None
  23. RETURN: No value
  24. */
  25. function playGame() {
  26. debugger;
  27. printBoard();
  28.  
  29. // Check if the board has enough positions left.
  30. if (checkBoard()) {
  31. console.log("GAME OVER!");
  32.  
  33. } else {
  34. // Ask the human player a position on the board, i.e., a number between 1 and 9.
  35. // Assume the user always enters a valid number.
  36. let humanNumber = parseInt(prompt("Enter a position on the board: "));
  37.  
  38. // Place the mark of the human player on the board
  39. placeMark(humanNumber, HUMAN_MARK);
  40.  
  41. // Check if the board has enough positions left.
  42. if (!checkBoard()) {
  43.  
  44. // Generate a mark for the computer player and place it on the board.
  45. generateComputerMove();
  46. }
  47. }
  48. }
  49. while(checkBoard()==false){
  50. playGame();
  51. }
  52.  
  53.  
  54. /************************************************************************************************************************
  55. TASK 1: Complete the printBoard() function.
  56. This function should print to the console (using console.log) the board with the marks for each player. "If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if
  57. the board is full, then this function should return false.
  58. Marks for the human player should be represented with an "X".
  59. Marks for the computer player should be represented with an "O" (capital letter "o").
  60. Empty positions on the board should be represented by their corresponding number (i.e., a number between 1 and 9).
  61.  
  62. PARAMETERS: None
  63. RETURN: No value
  64. *************************************************************************************************************************/
  65. function printBoard() {
  66. console.clear();
  67. console.log(BOARD[0][0] + " | " + BOARD[0][1] + " | "+BOARD[0][2]);
  68. console.log("---+---+---\n");
  69. console.log(BOARD[1][0] + " | " + BOARD[1][1] + " | "+BOARD[1][2]);
  70. console.log("---+---+---\n");
  71. console.log(BOARD[2][0] + " | " + BOARD[2][1] + " | "+BOARD[2][2]);
  72. }
  73.  
  74. /************************************************************************************************************************"If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if
  75. the board is full, then this function should return false.
  76. TASK 2: Complete the placeMark() function.
  77. This function should place a mark on the board for either player.
  78.  
  79. PARAMETERS: number: An integer value indicating the position on the board to place the mark (i.e., a number between 1 and 9).
  80. mark: An integer value indicating the mark to be placed (either -1 or 0).
  81. RETURN: A boolean value indicating whether the mark has been placed.
  82. If the mark was succesfully placed in the given position, then this function should return true.
  83. It should return false otherwise.
  84. *************************************************************************************************************************/
  85. function placeMark(number, mark) {
  86. for (var i=0;i<BOARD.length;i++){
  87. var innerArrayLength=BOARD[i].length;
  88. for(var j=0; j<innerArrayLength; j++){ //iterate multi-array
  89. if(BOARD[i][j] == parseInt(number, 10)){ //check values for match against selected position
  90. if(mark == -1){ //Check human
  91. BOARD[i][j] = "X"; //replace value
  92. }else if(mark == 0){ //Check computer
  93. BOARD[i][j] = "O" //replace value
  94. }
  95. return true; //successful insertion
  96. }else{
  97. return false; //falied insertion
  98. }
  99. }
  100. }
  101. }
  102.  
  103. /************************************************************************************************************************
  104. TASK 3: Complete the generateComputerMove() function.
  105. This function should generate a mark at random for the computer player and place it on the board.
  106. This function should call the placeMark() function.
  107.  
  108. PARAMETERS: None
  109. RETURN: No value
  110. *************************************************************************************************************************/
  111. function generateComputerMove() {
  112. var computerPosition = Math.floor((Math.random() * 9) + 1);
  113. if(placeMark(computerPosition, COMPUTER_MARK)==false){
  114. generateComputerMove(computerPosition,COMPUTER_MARK);
  115. }
  116. }
  117.  
  118. /*************************************************"If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if
  119. the board is full, then this function should return false.
  120. ***********************************************************************
  121. TASK 4: Complete the checkBoard() function.
  122. This function should inform whether the board has positions left to let both players keep placing
  123. their marks on the board.
  124.  
  125. PARAMETERS: None
  126. RETURN: A boolean value.
  127. If the board has at least one position empty, then this function should return true."If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if the board is full, then this function should return false.
  128.  
  129. *************************************************************************************************************************/
  130. function checkBoard() {
  131. debugger;
  132. if((BOARD[0][0]=="X" && BOARD[0][1]=="X" && BOARD[0][2]=="X") || (BOARD[0][0]=="O" && BOARD[0][1]=="O" && BOARD[0][2]=="O")){//top row
  133. return true;
  134. }else if((BOARD[1][0]=="X" && BOARD[1][1]=="X" && BOARD[1][2]=="X" ) || (BOARD[1][0]=="O" && BOARD[1][1]=="O" && BOARD[1][2]=="O" )){//middle row
  135. return true;
  136. }else if((BOARD[2][0]=="X" && BOARD[2][1]=="X" && BOARD[2][2]=="X" ) || (BOARD[2][0]=="O" && BOARD[2][1]=="O" && BOARD[2][2]=="O" )){//bottom row
  137. return true;
  138. }else if((BOARD[0][0]=="X" && BOARD[1][0]=="X" && BOARD[2][0]=="X" ) || (BOARD[0][0]=="O" && BOARD[1][0]=="O" && BOARD[2][0]=="O" )){//left column
  139. return true;
  140. }else if((BOARD[0][1]=="X" && BOARD[1][1]=="X" && BOARD[2][1]=="X" ) || (BOARD[0][1]=="O" && BOARD[1][1]=="O" && BOARD[2][1]=="O" )){//middle column
  141. return true;
  142. }else if((BOARD[0][2]=="X" && BOARD[1][2]=="X" && BOARD[2][2]=="X" ) || (BOARD[0][2]=="O" && BOARD[1][2]=="O" && BOARD[2][2]=="O" )){//right column
  143. return true;
  144. }else if((BOARD[0][0]=="X" && BOARD[1][1]=="X" && BOARD[2][2]=="X" ) || (BOARD[0][0]=="O" && BOARD[1][1]=="O" && BOARD[2][2]=="O" )){//top left diag
  145. return true;
  146. }else if((BOARD[0][2]=="X" && BOARD[1][1]=="X" && BOARD[2][0]=="X" ) || (BOARD[0][2]=="O" && BOARD[1][1]=="O" && BOARD[2][0]=="O" )){//top right diag
  147. return true;
  148. }
  149.  
  150. for (var i=0, len=BOARD.length; i<len; i++) {
  151. for (var j=0, len2=BOARD[i].length; j<len2; j++) {
  152. if (typeof BOARD[i][j] == 'number'){
  153. return false;//stop as soon as a number is found
  154. }
  155. }
  156. }
  157. return true;//if ive managed to iterate over all values without turning up true then one all board must be a "X" or "O"
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement