Eh, anyone can help me how to code for the c++ programming AI tic tac toe . I completely stuck like .........
Eh, anyone can help me how to code for the c++ programming AI tic tac toe . I completely stuck like .........
This is my code. I completely stuckCode:#include <iostream> using namespace std; int main() { char square[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}}; bool validrow, validcol, validmove; int row, col; char winner = ' '; char turn = 'X'; cout << "Welcome to tic tac toe game" << endl; while (winner == ' ') { cout << " 1 2 3" << endl; cout << " ---------------" << endl; for(int i = 1; i < 4; i++) { cout << " " << i << " | " << square[i-1][0] << " | " << square[i-1][1] << " | " << square[i-1][2] << " |" << endl << " ---------------" << endl; } validrow = false; validmove = false; validcol = false; while(!validmove) { validrow = false; //Loop until the player selects a valid row while(!validrow) { cout << "Row: "; cin >> row; if (row == 1 || row == 2 || row == 3) { validrow = true; } else { cout << endl << "Invalid row!" << endl; } } //Loop until the player selects a valid column while(!validcol) { cout << "Column: "; cin >> col; if(col == 1 || col == 2 || col == 3) { validcol = true; } else { cout << endl << "Invalid column!" << endl; } } validmove = true; bool validturn; validturn = false; while (!validturn) { if (square[row-1][col-1] == ' ') { square[row-1][col-1] = turn; if (turn == 'X') { turn = 'O'; } else { turn = 'X'; } if (row == 1 || row == 3 || col == 1 || col == 3 ) { square[1][1] = turn; cout << "Your next move" << endl; if (row == 1 || col == 1) { square[2][2] = turn; cout << "Your next move" << endl; validturn = true; } validturn = true; } else if (row == 2 || col == 2) { square[2][1] = turn; cout << "Your next move" << endl; validturn = true; } else { cout << "The selected square is occupied!" << endl; cout << "Select again:" << endl; } } } } } system ("pause"); return 0; }
any1 give reply on how to start on the tic tac game with first? Any steps before i trying coding. need help badly. Thank you.
I am not sure what you are asking, but as I see it, there is no AI implemented at all yet?
- Have you tried meta-planning?
- How does the computer win the game? 3 in-a-row, of course, so check if it can do that first ,and make the computer do it.
- What is the first step to getting to 3 in-a-row? 2 in-a-row. check to see if the computer can get two in a row, with the possibility to make it three in the next turn (No sense in going for two in a row, where the third one is already occupied by the enemy!).
- I'd also say, if the center wasn't occupied, go for that one first.
A little backwards, but I'm sure you can make sense of it
As my tutor said, there are no big problems in programming... only a lot of smaller ones! Break the big problems into small chunks and it will be soo much easier.
PS. I'm sorry I can't give you any examples, I'm new at this too
You need a checkWin function or piece of code before you start making any AI.
This topic has been covered several times before on this forum if you care to do a search.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
How to start of with? Any method on how should I start first.
Player against computer:
The current state of the board should be displayed at all times. The program should be able to detect a winning combination and stop the game at
that point. (This is my school question)
I just need a simple tic tac toe AI using only like arrays, control selection(if/else) and loops and not graphics