C++ Homework help regarding tic tac toe game.

I need to fill in the int main() portion of the following code where the first move is randomly chosen (computer or player) and then the game is played.
Given:

#include <iostream>
using namespace std;

// function prototypes
int computerMove(int[]);
void printBoard(int[]);

int main()
{
}

/*****************************************************************
* Use by computerMove to evaluate prospective tic-tac-toe move. *
* param board 9-element integer array representing game board. *
* param i, j indicates of previously placed board squares. *
* param k the index of the proposed move. *
* return true/false the proposed move is good. *
*****************************************************************/
bool goodMove(int board[], int i, int j, int k)
{
if ((board[i] == board[j]) && (board[i] != 0) && (board[k] == 0))
return true;
else return false;
}

/********************************************************************
* Logic by which computer player makes a tic-tac-toe move. *
* param board 9-element integer array representing game board. *
* return index (0 through 8) of computer's move or 9 if board full *
********************************************************************/
int computerMove(int board[])
{
// Examine all rows.
if (goodMove(board,0,1,2)) return 2;
else if (goodMove(board,0,2,1)) return 1;
else if (goodMove(board,1,2,0)) return 0;
else if (goodMove(board,3,4,5)) return 5;
else if (goodMove(board,3,5,4)) return 4;
else if (goodMove(board,4,5,3)) return 3;
else if (goodMove(board,6,7,8)) return 8;
else if (goodMove(board,6,8,7)) return 7;
else if (goodMove(board,7,8,6)) return 6;

// Examine all columns.
else if (goodMove(board,0,3,6)) return 6;
else if (goodMove(board,0,6,3)) return 3;
else if (goodMove(board,3,6,0)) return 0;
else if (goodMove(board,1,4,7)) return 7;
else if (goodMove(board,1,7,4)) return 4;
else if (goodMove(board,4,7,1)) return 1;
else if (goodMove(board,2,5,8)) return 8;
else if (goodMove(board,2,8,5)) return 5;
else if (goodMove(board,5,8,2)) return 2;

// Examine diagonals.
else if (goodMove(board,0,4,8)) return 8;
else if (goodMove(board,4,8,0)) return 0;
else if (goodMove(board,0,8,4)) return 4;
else if (goodMove(board,2,4,6)) return 6;
else if (goodMove(board,4,6,2)) return 2;
else if (goodMove(board,2,6,4)) return 4;

// Finally just pick a spot.
else if (board[4] == 0) return 4;
else if (board[0] == 0) return 0;
else if (board[2] == 0) return 2;
else if (board[6] == 0) return 6;
else if (board[8] == 0) return 8;
else if (board[1] == 0) return 1;
else if (board[3] == 0) return 3;
else if (board[5] == 0) return 5;
else if (board[7] == 0) return 7;

else return 9; // no legal move
}

/****************************************************************
* Print tic-tac-toe game board. *
* param board 9-element integer array representing game board. *
****************************************************************/
void printBoard(int board[])
{
for (int i = 0; i < 9; i++) {
if (board[i] == 1) cout << " X ";
else if (board[i] == 2) cout << " O ";
else cout << " ";
if (i < 8) {
if (i % 3 == 2) cout << "\n---|---|---" << endl;
else cout << "|";
}
else cout << endl << endl;
}
}




I have added #include <cstdlib> and #include <ctime>
as well as the following: What do I do? I am so lost and it's been a few days. I am a beginner and this is too hard for me. P.S. Please add details so I can learn too! I have not finished a few of the loops, but I'm stuck on how to insert the chosen move, as well as how to finish my loops and print it out to the screen. Am I even approaching it correctly?

int main()
{int board[9]; // the tic-tac-toe board
int playerMove, computerMove, cmove, r, moveFirst;
char i, j, k;
srand(time(0));
cout << "Let's play a really fun game! Computer is [X] and you will be [O]...let's go!" << endl;
cout << "The board looks like this: \n 012 \n 345 \n 678" << endl;

srand(time(0));
r = rand() % 2;
do {if (r = 0)//Test who goes first.
{
cout << "The computer will go first!" << endl;
computerMove = rand() % 9;

}
else {cout << "Yay! You get to go first! Pick a number 0-8" << endl;}
cin >> playerMove;
do (playerMove >= 0 && playerMove < 9)
{

}

} while (i != false || j != false || board[9] != false);
return 0;
}
Lines 8-10: Why are you declaring an empty main here?

Lines 105,109: Why are you calling srand() twice?
You're calling time(), but have not included the <ctime> header.

Line 111: You're using the assignment operator (=), not the equality operator (==).

Line 119: You're trying to put a conditonal expression after do. I think you probably want a while statement here.

Line 125: You're trying to test i, but i has never been initialized. Also, you shouldn't be comparing a char to a bool.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.





Last edited on
Topic archived. No new replies allowed.