Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 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. // Print the board
  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. playGame();
  50.  
  51. /************************************************************************************************************************
  52. TASK 1: Complete the printBoard() function.
  53. 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
  54. the board is full, then this function should return false.
  55. Marks for the human player should be represented with an "X".
  56. Marks for the computer player should be represented with an "O" (capital letter "o").
  57. Empty positions on the board should be represented by their corresponding number (i.e., a number between 1 and 9).
  58.  
  59. PARAMETERS: None
  60. RETURN: No value
  61. *************************************************************************************************************************/
  62. function printBoard() {
  63. console.clear();
  64. console.log(BOARD[0][0] + " | " + BOARD[0][1] + " | "+BOARD[0][2]);
  65. console.log("---+---+---\n");
  66. console.log(BOARD[1][0] + " | " + BOARD[1][1] + " | "+BOARD[1][2]);
  67. console.log("---+---+---\n");
  68. console.log(BOARD[2][0] + " | " + BOARD[2][1] + " | "+BOARD[2][2]);
  69. }
  70.  
  71. /************************************************************************************************************************"If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if
  72. the board is full, then this function should return false.
  73. TASK 2: Complete the placeMark() function.
  74. This function should place a mark on the board for either player.
  75.  
  76. PARAMETERS: number: An integer value indicating the position on the board to place the mark (i.e., a number between 1 and 9).
  77. mark: An integer value indicating the mark to be placed (either -1 or 0).
  78. RETURN: A boolean value indicating whether the mark has been placed.
  79. If the mark was succesfully placed in the given position, then this function should return true.
  80. It should return false otherwise.
  81. *************************************************************************************************************************/
  82. function placeMark(number, mark) {
  83. for (var i=0;i<BOARD.length;i++){
  84. var innerArrayLength=BOARD[i].length;
  85. for(var j=0; j<innerArrayLength; j++){
  86. if(BOARD[i][j] == parseInt(number, 10)){
  87. if(mark==)
  88. }
  89. }
  90. }
  91. }
  92.  
  93. /************************************************************************************************************************
  94. TASK 3: Complete the generateComputerMove() function.
  95. This function should generate a mark at random for the computer player and place it on the board.
  96. This function should call the placeMark() function.
  97.  
  98. PARAMETERS: None
  99. RETURN: No value
  100. *************************************************************************************************************************/
  101. function generateComputerMove() {}
  102.  
  103. /*************************************************"If a player has placed three marks in a row, either horizontally, vertically, or diagonally, or if
  104. the board is full, then this function should return false.***********************************************************************
  105. TASK 4: Complete the checkBoard() function.
  106. This function should inform whether the board has positions left to let both players keep placing
  107. their marks on the board.
  108.  
  109. PARAMETERS: None
  110. RETURN: A boolean value.
  111. 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
  112. the board is full, then this function should return false.
  113. If a player has player has placed three marks in a row, either horizontally, vertically, or diagonally, or if
  114. the board is full, then this function should return true.
  115. *************************************************************************************************************************/
  116. function checkBoard() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement