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!
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;
int board[9]; // the tic-tac-toe board
srand(time(0));
if (rand() % 2 = 0 && r = 0)//Test who goes first.
{
moveFirst = 1;
cout << "The computer will go first!" << endl;
}
else {cout << "Yay! You get to go first!" << endl;}
do{
} while (i != false || j != false || board[9] !=);
return 0;}
The key to programming is to break things down into simple steps that can be expressed in lines of code. If you are having a hard time interpreting the code, go ahead and ask specific questions (or give us specific errors). Here are some pointers to get you started:
Important note:
Computer generated random numbers aren't really random. They use a seed (starting value) and a couple of special numbers in a calculation that SEEMS to produce random numbers. In order to produce a different set of numbers, a different seed must be used. Which is why we seed the generator with the time (because it is always different as time moves on). You don't need to seed it twice unless you have a specific reason for doing so.
Your main loop should do the following:
Set things up:
1. The first thing you need to do is initialize your board. Whenever you set aside memory to be used by a variable, that memory space can have any value. So in order to make sure it has a predictable value, you must first "initialize" your variable. CUrrently your board array of integers has a set of arbitrary ("random") values.
2. Since you need a variable to keep track of whose turn it is, you don't really need a "moveFirst" variable. You simply assign the turn variable to the appropriate player (1 for computer, 0 for human) at the beginning. Again... it is a good idea to make sure the variable is assigned a value you expect.
Execute a turn loop:
1. You need to decide whose turn it is.
2. You need to get the move using an appropriate method depending on whether it is the humans or the computers turn.
3. You need to figure out when the loop should be exited.
Finish:
Tell the human who won. [EDIT: it doesn't look like the assignment provides a step to figure this out. So you either need to write that method yourself, or skip this step if it is not required]
Try breaking down the above steps and writing the code yourself. If you have trouble or need help understanding the code you were given, ask a specific question.
FYI your current while statement has a few issues.
Yes. That is correct. However you need your random number to be a number between 0 and 8 (a number between 0 and 8 that represents each cell in the tic tac toe board). As is that won't work.
If you notice whenever the random number generator is used by the given code it is usually used with the modulus operator (%). This means divide by the number and take the remainder. This is what allows you to put a limit on your random number:
x = rand() % n;
The result will always be a number between 0 and n but not including n because n can never be a remainder. (A remainder of n means the number is evenly divisible by n which means your remainder will be 0).
board[9] will only create an array with indexes 0-8. You need to initialize each one of those to an expected value. Otherwise they could be already set to 1 or 2 which means X or O respectively. (It is unlikely but possible).
Now that I have who goes first based on whether or not a random number is even or odd, and I'm going to use your idea on "randomly" taking 0-8 for each computer move, how do I 'insert' it into the table? I know it has something to do with the output at the bottom of the given text, but getting it to print out has stumped me.
I have done a do-while with if/else inside, is this even "legal" in c++?
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;
}
else {cout << "Yay! You get to go first!" << endl;}
} while (i != false || j != false || board[9] != false);
return 0;
}