TicTacToe Project 2

So this is a follow up to a post I did a while ago. I'm stuck with this project.
I have no idea what to do next. These are the instructions:
Programming and Problem Solving
Project 3 — Tic-Tac-Toe Player
Goal
Create a program that plays a game of tic-tac-toe between a human and the computer.
Details
The program should allow the computer to play either X (who moves first) or O (who moves second).
When it is the human’s turn to play, it should display the board and ask for a move. Display a tic-tactoe
board with filled-in squares displaying an X or O as appropriate and a digit 1–9 for any unfilled
squares. Squares are numbered 1, 2, 3 across the top row, 4–6 across the middle row and 7–9 across
the bottom row.
The human’s move must be validated; that is, make sure that the chosen square is unoccupied, to
prevent the human from cheating.
The computer should always select its move according to the following steps:
1. If the computer can win, select the winning square.
2. Otherwise, if the computer can block the human from winning, select the blocking square.
3. Otherwise, choose the center square (square 5) if it’s open.
4. Otherwise, choose any open corner square (squares 1, 3, 7 or 9).
5. Otherwise, choose any open square (squares 2, 4, 6 or 8).
Select the X player at random.
.Suggestions and Hints
• Use a variable board[3][3] to store the board. Since the board is needed by all parts of
the program, it can be a global variable.
• You will want to create the following functions:
– int score(int cell1,int cell2,int cell3)
Using +1 for an X, −1 for an O and 0 for an empty cell, add the values for the three
specified cells and return the sum.
– bool playerWins(char player)
Returns true if the given player has won, false if they have not currently won (note: this
doesn’t necessarily mean that the player has lost.)
Programming and Problem Solving Project 3 — Tic-Tac-Toe Fall 2018 — CRN 41441
– bool canWin(char player,int &square)
This function should look at the board and return true if the given player (’X’ or ’O’) can
win on this move and return false if the player cannot win on this move. Fill in the appropriate
square if the player can win on this move.
Hint: What’s the difference between canWin(’X’,sq) and canBlock(’O’,sq)?
– void printBoard(void)
Displays the current board (see above.)
– int getComputerMove(char player)
Gets the computer’s move as either X or O according to the algorithm described above.
– int getHumanMove(char player)
Gets the human’s move as either X or O. Also validates the move to make sure it’s legal.
-------------------
Here is what i have done so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;
char player;
char board[3][3] = 
  {'1','2','3','4','5','6','7','8','9'};
void printBoard()
{
  cout << "TicTacToe: Please Select X or O" << endl<< "---------------" << endl;
  for(int x = 0; x < 3; x++)
  {
    for(int y = 0; y < 3; y++)
    {
      
      cout << "| " << board[x][y] << " |";
    }
    cout << endl << "---------------" << endl;
  }
}  


int getHumanmove()
{
  char hm;
  cout << "Select an open space: ";
  cin >> hm;
  if(hm == '1')
   board[0][0] = player;
  else if(hm == '2')
   board[0][1] = player;
  else if(hm == '3')
   board[0][2] = player;
  else if(hm == '4')
   board[1][0] = player;
  else if(hm == '5')
   board[1][1] = player;
  else if(hm == '6')
   board[1][2] = player;  
  else if(hm == '7')
   board[2][0] = player;
  else if(hm == '8')
   board[2][1] = player;
  else if(hm == '9')
   board[2][2] = player;
  else if(hm == player || cpu)
   cout << "Invalid Input!";
  else{
  cout << "Invalid Input!";}
return 1; 
}

int main()
{
  printBoard();
  getHumanmove();

return 0;
}

Last edited on
> • You will want to create the following functions:
So start by doing this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Using +1 for an X, −1 for an O and 0 for an empty cell, add the values for the three
// specified cells and return the sum.
int score(int cell1,int cell2,int cell3) {
    return 0;
}

// Returns true if the given player has won, false if they have not currently won (note: this
// doesn’t necessarily mean that the player has lost.)
bool playerWins(char player) {
    return false;
}

// This function should look at the board and return true if the given player (’X’ or ’O’) can
// win on this move and return false if the player cannot win on this move. Fill in the appropriate
// square if the player can win on this move.
// Hint: What’s the difference between canWin(’X’,sq) and canBlock(’O’,sq)?
bool canWin(char player,int &square) {
    return false;
}

// Gets the computer’s move as either X or O according to the algorithm described above.
int getComputerMove(char player) {
    return 0;
}

// Gets the human’s move as either X or O. Also validates the move to make sure it’s legal.
int getHumanMove(char player) {
    return 0;
}

int main ( ) {
    return 0;
}

It won't do anything, but at least you can make sure it compiles.

Then you might do something like
1
2
3
4
5
6
7
char board[3][3] = 
  {'O','X','3','X','X','6','O','8','O'};
///
int main ( ) {
    printBoard();
    cout << "score for first row=" << score(0,1,2) << endl;
}

This is called incremental development and testing.
Write a lot more tests for score and make sure it WORKS before tackling the next problem.

Then you might think about, how can I use score() to implement playerWins()?

It's all about doing one thing at a time, making sure you understand the smaller steps, and making sure things work along the way.

You don't just stare at the whole problem, hoping that at some point you get a sudden flash of inspiration and voilà, the answer.

Nor is it a case of typing in 100's of lines of code at once, then wondering why it doesn't even compile.
Topic archived. No new replies allowed.