TicTacToe in C++ won't work? Help please - c++

So I'm trying to get this simple Multiplayer TicTacToe game to work in eclipse (C++ version), but it seems that whenever I run my program, only for the top row of a TicTacToe can people win.
Here's my code:
#include <iostream>
using namespace std;
string board[3][3]; // creates the game board
void draw(int player, int num)
{
num--;
int row = num/3;
int col = num%3;
if ( player == 1 ) board[row][col] = "X";
else board[row][col] = "O";
}// end draw method
bool checkWin(int num)
{
bool win = false;
if (num == 1 || num == 2 || num == 3)
{
if ( board[0][0] == board[0][1] && board[0][0] == board[0][2])
return true;
}
if (num == 4 || num == 5 || num == 6)
{
if ( board[1][0] == board[1][1] && board[1][0] == board[1][2])
return true;
}
if (num == 7 || num == 8 || num == 9)
{
if ( board[2][0] == board[2][1] && board[2][0] == board[2][2])
return true;
}
if (num == 1 || num == 4 || num == 7)
{
if ( board[0][0] == board[1][0] && board[0][0] == board[2][0])
return true;
}
if (num == 2 || num == 5 || num == 8)
{
if ( board[0][1] == board[1][1] && board[0][1] == board[2][1])
return true;
}
if (num == 3 || num == 6 || num == 9)
{
if ( board[0][2] == board[1][2] && board[0][2] == board[2][2])
return true;
}
if (num == 1 || num == 5 || num == 9)
{
if ( board[0][0] == board[1][1] && board[0][0] == board[2][2])
return true;
}
if (num == 3 || num == 5 || num == 7)
{
if ( board[0][2] == board[1][1] && board[0][2] == board[2][0])
return true;
}
return win;
} // end checkWin method
int main()
{
cout << "C++ TIC TAC TOE GAME" << endl;
cout << "The board is labeled from 1-9 in row-major order." << endl;
bool winner = false;
int turns = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = "-";
cout << board[i][j] << " ";
}
cout << endl; // "enters" to next line
} // prints the game board 2d array
while (winner == false && turns < 9)
{
int player = 0;
int placedAt = 0;
if ( turns % 2 == 0 )
{
int num1;
cout << "Player 1 enter what space to place X: ";
cin >> num1;
player = 1;
draw(player,num1);
placedAt = num1;
}
else
{
int num2;
cout << "Player 2 enter what space to place O: ";
cin >> num2;
player = 2;
draw(player,num2);
placedAt = num2;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << board[i][j] << " ";
}
cout << endl; // "enters" to next line
} // prints the game board 2d array
winner = checkWin(placedAt);
if (winner == true)
{
cout << endl << "Player " << player << " WINS!";
}
turns++;
cout << endl;
} // end while loop
if (winner == false) cout << endl << "NO ONE WON!";
return 0;
} // end main method

Change string board[3][3]; to char board[3][3]; , and "X" to 'X'.
The corrected code below should compile just fine.
#include <iostream>
using namespace std;
char board[3][3]; // creates the game board
void draw(int player, int num)
{
num--;
int row = num/3;
int col = num%3;
if ( player == 1 ) board[row][col] = 'X';
else board[row][col] = 'O';
}// end draw method
bool checkWin(int num)
{
bool win = false;
if (num == 1 || num == 2 || num == 3)
{
if ( board[0][0] == board[0][1] && board[0][0] == board[0][2])
return true;
}
if (num == 4 || num == 5 || num == 6)
{
if ( board[1][0] == board[1][1] && board[1][0] == board[1][2])
return true;
}
if (num == 7 || num == 8 || num == 9)
{
if ( board[2][0] == board[2][1] && board[2][0] == board[2][2])
return true;
}
if (num == 1 || num == 4 || num == 7)
{
if ( board[0][0] == board[1][0] && board[0][0] == board[2][0])
return true;
}
if (num == 2 || num == 5 || num == 8)
{
if ( board[0][1] == board[1][1] && board[0][1] == board[2][1])
return true;
}
if (num == 3 || num == 6 || num == 9)
{
if ( board[0][2] == board[1][2] && board[0][2] == board[2][2])
return true;
}
if (num == 1 || num == 5 || num == 9)
{
if ( board[0][0] == board[1][1] && board[0][0] == board[2][2])
return true;
}
if (num == 3 || num == 5 || num == 7)
{
if ( board[0][2] == board[1][1] && board[0][2] == board[2][0])
return true;
}
return win;
} // end checkWin method
int main()
{
cout << "C++ TIC TAC TOE GAME" << endl;
cout << "The board is labeled from 1-9 in row-major order." << endl;
bool winner = false;
int turns = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = '-';
cout << board[i][j] << ' ';
}
cout << endl; // "enters" to next line
} // prints the game board 2d array
while (winner == false && turns < 9)
{
int player = 0;
int placedAt = 0;
if ( turns % 2 == 0 )
{
int num1;
cout << "Player 1 enter what space to place X: ";
cin >> num1;
player = 1;
draw(player,num1);
placedAt = num1;
}
else
{
int num2;
cout << "Player 2 enter what space to place O: ";
cin >> num2;
player = 2;
draw(player,num2);
placedAt = num2;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << board[i][j] << " ";
}
cout << endl; // "enters" to next line
} // prints the game board 2d array
winner = checkWin(placedAt);
if (winner == true)
{
cout << endl << "Player " << player << " WINS!";
}
turns++;
cout << endl;
} // end while loop
if (winner == false) cout << endl << "NO ONE WON!";
return 0;
} // end main method

Related

C++ Erasing Vector element

vector<char>teikums(count);
Having problems when trying to erase a element from vector.
For Example i input:
a b c d e f
and when trying to erase a element
teikums.erase(teikums.begin() + 3);
it will output
a b d e e f
Been trying to figure out why it doesn't output
a b c d f
Code:
int main() {
system("cls"); // Notira Ekranu
patsk = 0; // Pieskir vertibu
cout << "Ievadi Massiva lielumu: ";
cin >> count;
vector<char>teikums(count);
cout << "Vai aizpildit burtus automatiski (Y/Cits): ";
cin >> aizp;
srand(time(NULL));
cout << "\n";
if (aizp == 'y' || aizp == 'Y') {
for (int i = 1; i <= count; i++) {
teikums[i] = (rand() % 26) + 'a';
cout << teikums[i] << " ";
}
}
else {
do {
cout << "Ievadi " << count << " burtus vienu pa vienam\n";
for (i = 1; i <= count; i++) {
cin >> teikums[i];
if (!((teikums[i] >= 'a' && teikums[i] <= 'z') || (teikums[i] >=
'A' && teikums[i] <= 'Z'))) {
cout << "Kluda! Ievadiet tikai burtus\n";
i = i - 1;
}
}
}
while (i <= count);
}
teikums.erase(teikums.begin() + 3);
do {
cout << "\n";
cout << "\n1.Izpildit individualo uzdevumu";
cout << "\n2.Pievienot jaunu elementu vektoram";
cout << "\n3.Dzest elementu no vektroa";
cout << "\n4.Sakt programmu no jauna";
cout << "\n5.Beigt Darbu";
cout << "\nIevadi izveli : ";
cin >> opcijas;
switch (opcijas) {
case 1:
izpildit_uzdevumu(teikums);
break;
case 2:
pievienot_elementu(teikums, count);
break;
case 3:
dzest_elementu(teikums);
break;
case 4:
no_jauna();
break;
case 5:
return 0;
}
}
while (opcijas != 4);
getch();
}
void izpildit_uzdevumu(vector<char>& teikums) {
patskani = 0;
for (i = 0; i <= count; i++) {
cout << " " << teikums[i];
}
cout << "\nIzmantotie Patskani:";
for (i = 1; i < count; i = i + 2) {
if (teikums[i] == 'a' || teikums[i] == 'e' || teikums[i] == 'i' ||
teikums[i] == 'o' || teikums[i] == 'u' || teikums[i] == 'A' ||
teikums[i] == 'E' || teikums[i] == 'I' || teikums[i] == 'O' ||
teikums[i] == 'U') {
patskani = patskani + 1;
cout << teikums[i];
}
}
cout << "\nPatskanu Skaits: " << patskani;
}
When you erase the element, you should also decrease count, or use teikums.size() to monitor the size of your vector.
You had 6 elements, you did:
teikums.erase(teikums.begin() + 3);
which erases the 4th element, thus now the vector has 5 elements in number.
Check the example in the ref too.

C++ Small Straight (like yahtzee or poker). 5 Dice roll (1234)/(2345)/(3456)

Im a beginner with C++ and this is for homework but Im stuck. I have one remaining problem and then im finished.
I can'T think of an algorithm that will tell if the user entered a small straight (1234) or (2345) or (3456).
I know how to do it using a loop but is it possible to not use a loop? When this was assigned, we did not learned about loops yet. I think we are only suppose to use if statements.
I started coding for it (I wrote what the dice can be, but cant figure out how to let only 4 dice go straight without an array and loop). Im stuck! Please help!
Can you also check at my code and see if there is anything wrong so far.
Assigned Problem:
Playing poker with dice. (Kinda like Yahtzee)
• Have the user enter 5 dice rolls (1-6) in any order. Have the user type in 5 numbers. If any are outside of the 1 to 6 inclusive range, just end the program.
• Count and print the number of ones entered.
• Count and print the number of twos entered.
• Count and print the number of threes entered.
• Count and print the number of fours entered.
• Count and print the number of fives entered.
• Count and print the number of sixes entered.
• Tell if there are three or more dice the same.
• Tell if there are four or more dice the same.
• Tell if there are five dice the same.
• Tell if there is a full house, 3 the same and 2 the same.
• Tell if there is a long straight ... 1 2 3 4 5 or 2 3 4 5 6
• Tell if there is a small straight (1 2 3 4) or (2 3 4 5) or (3 4 5 6)
My Code:
int main(int argc, char** argv)
{
//
int Roll1, Roll2, Roll3, Roll4, Roll5;
int numberOf1s = 0, numberOf2s = 0, numberOf3s = 0, numberOf4s = 0, numberOf5s = 0, numberOf6s = 0;
bool TwoOfaKind = false;
bool ThreeOfaKind = false;
cout << "Roll 5 dice. Enter them below." << endl;
cout << "Roll 1: ";
cin >> Roll1;
cout << "Roll 2: ";
cin >> Roll2;
cout << "Roll 3: ";
cin >> Roll3;
cout << "Roll 4: ";
cin >> Roll4;
cout << "Roll 5: ";
cin >> Roll5;
if (Roll1 > 6 || Roll1 < 1 || Roll2 > 6 || Roll2 < 1 || Roll3 > 6 || Roll3 < 1 || Roll4 > 6 || Roll4 < 1 || Roll5 > 6 || Roll5 < 1 )
{
exit(1);
}
if (Roll1 == 1)
{
numberOf1s += 1;
}
if (Roll2 == 1)
{
numberOf1s += 1;
}
if (Roll3 == 1)
{
numberOf1s += 1;
}
if (Roll4 == 1)
{
numberOf1s += 1;
}
if (Roll5 == 1)
{
numberOf1s += 1;
}
cout << "There are " << numberOf1s << " ones." << endl;
if (Roll1 == 2)
{
numberOf2s += 1;
}
if (Roll2 == 2)
{
numberOf2s += 1;
}
if (Roll3 == 2)
{
numberOf2s += 1;
}
if (Roll4 == 2)
{
numberOf2s += 1;
}
if (Roll5 == 2)
{
numberOf2s += 1;
}
cout << "There are " << numberOf2s << " twos." << endl;
if (Roll1 == 3)
{
numberOf3s += 1;
}
if (Roll2 == 3)
{
numberOf3s += 1;
}
if (Roll3 == 3)
{
numberOf3s += 1;
}
if (Roll4 == 3)
{
numberOf3s += 1;
}
if (Roll5 == 3)
{
numberOf3s += 1;
}
cout << "There are " << numberOf3s << " threes." << endl;
if (Roll1 == 4)
{
numberOf4s += 1;
}
if (Roll2 == 4)
{
numberOf4s += 1;
}
if (Roll3 == 4)
{
numberOf4s += 1;
}
if (Roll4 == 4)
{
numberOf4s += 1;
}
if (Roll5 == 4)
{
numberOf4s += 1;
}
cout << "There are " << numberOf4s << " fours." << endl;
if (Roll1 == 5)
{
numberOf5s += 1;
}
if (Roll2 == 5)
{
numberOf5s += 1;
}
if (Roll3 == 5)
{
numberOf5s += 1;
}
if (Roll4 == 5)
{
numberOf5s += 1;
}
if (Roll5 == 5)
{
numberOf5s += 1;
}
cout << "There are " << numberOf5s << " fives." << endl;
if (Roll1 == 6)
{
numberOf6s += 1;
}
if (Roll2 == 6)
{
numberOf6s += 1;
}
if (Roll3 == 6)
{
numberOf6s += 1;
}
if (Roll4 == 6)
{
numberOf6s += 1;
}
if (Roll5 == 6)
{
numberOf6s += 1;
}
cout << "There are " << numberOf6s << " sixes." << endl << endl;
if (numberOf1s == 2 || numberOf2s == 2 || numberOf3s == 2 || numberOf4s == 2 || numberOf5s == 2 || numberOf6s == 2)
{
TwoOfaKind = true;
}
if (numberOf1s == 3 || numberOf2s == 3 || numberOf3s == 3 || numberOf4s == 3 || numberOf5s == 3 || numberOf6s == 3)
{
cout << "Yes three of a kind!" << endl;
ThreeOfaKind = true;
}
else
{
cout << "No three of a kind" << endl;
}
if (numberOf1s == 4 || numberOf2s == 4 || numberOf3s == 4 || numberOf4s == 4 || numberOf5s == 4 || numberOf6s == 4)
{
cout << "Yes four of a kind!" << endl;
}
else
{
cout << "No four of a kind" << endl;
}
if (numberOf1s == 5 || numberOf2s == 5 || numberOf3s == 5 || numberOf4s == 5 || numberOf5s == 5 || numberOf6s == 5)
{
cout << "Yes five of a kind!" << endl;
}
else
{
cout << "No five of a kind" << endl;
}
//
if (TwoOfaKind == true && ThreeOfaKind == true)
{
cout << "Yes Full House!" << endl;
}
else
{
cout << "No Full House" << endl;
}
//
if (Roll1 == Roll2 && Roll2 == Roll3 && Roll3 == Roll4 && Roll4 == Roll5)
{
cout << "No Long Straight" << endl;
}
else
{
if (((Roll1 >= 1 && Roll1 < 6) && (Roll2 >= 1 && Roll2 < 6) && (Roll3 >= 1 && Roll3 < 6) && (Roll4 >= 1 && Roll4 < 6) && (Roll5 >= 1 && Roll5 < 6)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Long Straight!" << endl;
}
else if (((Roll1 > 1 && Roll1 <= 6) && (Roll2 > 1 && Roll2 <= 6) && (Roll3 > 1 && Roll3 <= 6) && (Roll4 > 1 && Roll4 <= 6) && (Roll5 > 1 && Roll5 <= 6)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Long Straight!" << endl;
}
else
{
cout << "No Long Straight" << endl;
}
//***HELP ME HERE PLEASE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***
if (Roll1 == Roll2 && Roll2 == Roll3 && Roll3 == Roll4 && Roll4 == Roll5)
{
cout << "No Small Straight" << endl;
}
else
{
if (((Roll1 > 1 && Roll1 <= 4) && (Roll2 > 1 && Roll2 <= 4) && (Roll3 > 1 && Roll3 <= 4) && (Roll4 > 1 && Roll4 <= 4) && (Roll5 > 1 && Roll5 <= 4)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Small Straight!" << endl;
}
else if (((Roll1 > 1 && Roll1 <= 5) && (Roll2 > 1 && Roll2 <= 5) && (Roll3 > 1 && Roll3 <= 5) && (Roll4 > 1 && Roll4 <= 5) && (Roll5 > 1 && Roll5 <= 5)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Small Straight!" << endl;
}
else if (((Roll1 > 2 && Roll1 <= 6) && (Roll2 > 2 && Roll2 <= 6) && (Roll3 > 2 && Roll3 <= 6) && (Roll4 > 2 && Roll4 <= 6) && (Roll5 > 2 && Roll5 <= 6)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Small Straight!" << endl;
}
else
{
cout << "No Small Straight" << endl;
}
}
return 0;
}
int bits = (1 << Roll1) | (1 << Roll2) | (1 << Roll3) | (1 << Roll4) | (1 << Roll5);
if((bits & 0x1E) == 0x1E || (bits & 0x3C) == 0x3C || (bits & 0x78) == 0x78)
cout << "Yes Small Straight!" << endl;
I decided to rewrite your program which refactors a lot of the code and also solves your problem. Do note I assumed you are using c++ but not necessarily c++11.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int straight_checker = 0;
int rolls[5] = {0}, num_count[6] = {0};
// use char * die_names[6] if c
string die_names[6] = {"ones", "twos", "threes", "fours", "fives", "sixes"};
bool two_kind = false, three_kind = false, four_kind = false, five_kind = false;
cout << "Roll 5 dice. Enter them below." << endl;
for(unsigned int i = 0; i < sizeof(rolls) / sizeof(int); ++i)
{
cout << "Roll " << i + 1 << ": ";
cin >> rolls[i];
if(rolls[i] < 1 || rolls[i] > 6)
{
exit(1);
}
else
{
num_count[rolls[i] - 1]++;
// This will set a binary "1" at the location rolls[i] - 1, easy way to check straights
straight_checker ^= (-1 ^ straight_checker) & (1 << (rolls[i] - 1));
}
}
for(unsigned int i = 0; i < sizeof(num_count) / sizeof(int); ++i)
{
cout << endl << "There are " << num_count[i] << " " <<die_names[i];
// If you are using c++11, then use std::find outside loop
switch(num_count[i])
{
case 2:
two_kind = true;
break;
case 3:
three_kind = true;
break;
case 4:
four_kind = true;
break;
case 5:
five_kind = true;
break;
}
}
cout << endl;
if(four_kind) cout << "Yes four of a kind!" << endl;
if(five_kind) cout << "Yes five of a kind!" << endl;
if(two_kind && three_kind) cout << "Yes Full House!" << endl;
else
{
if(three_kind) cout << "Yes three kind" << endl;
else if(two_kind) cout << "Yes two kind" << endl;
}
// 31 in binary is 011111, and 62 is 111110 (ie 5 straight 1s)
if(straight_checker == 31 || straight_checker == 62)
{
cout << "Yes long straight!" << endl;
}
// 47 in binary is 101111, 30 is 011110, 61 is 111101, etc (ie 4 straight 1s)
if(straight_checker == 15 || straight_checker == 47 || straight_checker == 30 || straight_checker == 60 || straight_checker == 61)
{
cout << "Yes short straight" << endl;
}
return 0;
}
I combine all the rolls in a single variable.
A 32 bits long can easily hold all the counts:
//Number of ones V
long result = 0x000000;
// sixes ^
Each hexadecimal digit can hold a value between 0 and F (15) so that's sufficient for 15 rolls, and you only need 5.
Your long straight would be 0x111110 or 0x011111. A full house 555-33 is 0x030200. A short straight is a bit more complex, it looks like 0x101111 or 0x001121. To get rid of that 2, I'd use a bit shift:
long test = (result | (result>>1)) & 0x777777 and then it's a straightforward check: test & 0x001111 == 0x001111 etc.

Tic Tac Toe Tie Condition C++

I can't seem to get this function to work correctly. You would assume since I check for vertical, horizontal and diagonal if those don't get a win it would be a tie, but my program says its a tie after I fill in the Top left box and the middle box. Any suggestions? Do I have to change the way I search for vertical horizontal and diagonal?
Here is my code for this function:
char winningLetter;
for (int i = 0; i < 3; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
winningLetter = board[i][0];
}
}
for (int i = 0; i < 3; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
{
winningLetter = board[0][i];
}
}
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0]))
{
winningLetter == board[1][1];
}
if (winningLetter == 'X')
{
cout << "Player One is the winner!" << endl;
}
else if (winningLetter == 'O')
{
cout << "Player Two is the winner!" << endl;
}
else
{
cout << "The game is a tie!" << endl;
tie = true;
}
return;
The problem is having the wrong conditions for a tie.
The condition is not
There is no winner
but
The game is over and there is no winner
You never checked if the game is over.

C++ Tic Tac Toe AI

I am almost done with my tic tac toe game. Currently it is set up as two-player person vs. person but I know I'll have to implement a simple AI to be approved. Now I need your help with this. I know I'll have to think about it in small steps such as three "make a move" methods like
If AI has a move in the 1st column && the two box to the right is open make a move in either box and return true
If AI has a move in the middle && the box to the left and right is open make a move in either box and return true
If AI has a move in the 3rd column && the two box to the left is open make a move in either box and return true
I can't understand exactly how I can implement it in my code below:
#include <iostream>
using namespace std;
char matrix[3][3] = { '7', '8', '9', '4', '5', '6', '1', '2', '3' };
char player = 'X';
int n;
void Draw()
{
system("cls");
cout << "Tic Tac Toe !\n" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
void Input()
{
int a;
cout << "\nIt's " << player << " turn. " << "Press the number of the field: ";
cin >> a;
if (a == 7)
{
if (matrix[0][0] == '7')
matrix[0][0] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 8)
{
if (matrix[0][1] == '8')
matrix[0][1] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 9)
{
if (matrix[0][2] == '9')
matrix[0][2] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 4)
{
if (matrix[1][0] == '4')
matrix[1][0] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 5)
{
if (matrix[1][1] == '5')
matrix[1][1] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 6)
{
if (matrix[1][2] == '6')
matrix[1][2] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 1)
{
if (matrix[2][0] == '1')
matrix[2][0] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 2)
{
if (matrix[2][1] == '2')
matrix[2][1] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 3)
{
if (matrix[2][2] == '3')
matrix[2][2] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
}
void TogglePlayer()
{
if (player == 'X')
player = 'O';
else
player = 'X';
}
char Win()
{
//first player
if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
return 'X';
if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
return 'X';
if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
return 'X';
if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
return 'X';
//second player
if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
return 'O';
if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
return 'O';
if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
return 'O';
if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
return 'O';
return '/';
}
int main()
{
n = 0;
Draw();
while (1)
{
n++;
Input();
Draw();
if (Win() == 'X')
{
cout << "X wins!" << endl;
break;
}
else if (Win() == 'O')
{
cout << "O wins!" << endl;
break;
}
else if (Win() == '/' && n == 9)
{
cout << "It's a draw!" << endl;
break;
}
TogglePlayer();
}
system("pause");
return 0;
}
A computer player for a simple board game like Tic-Tac-Toe can be implemented using the Minimax algorithm, which can be improved using α-β-pruning. Although the resulting implementation will be quite small, it might require some time to understand.
#include <iostream>
#include <string>
using namespace std;
int main()
{
/*
the chart is :
X 2 X
4 5 6
X 8 X
*/
char Matrix[3][3] = { 'X','2','X','4','5','6','X','8','X' };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << Matrix[i][j] << " ";
}
cout << endl;
}
/*
after this for :
X O X
4 5 6
X O X
*/
int l = 0, m = 0, p[3] = { 0,0,0 };
for (l; l <= 2; l++)
{
for (m; m <= 2; m++)
{
if ((Matrix[l][m]) == 'X')
{
p[l]++;
if ((p[l]) == 2)
{
for (m; m >= 0; m--)
{
if ((Matrix[l][m]) != 'X')
{
Matrix[l][m] = 'O';
}
}
}
}
}
}
return 0;
}
why this code dosent work?

C++ TicTacToe errorClass problems [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to learn C++ and I have made litte tictactoe game but somethings wrong. I've tried to make a error bool. But somethings wrong with it. I've tried for 2 hours now and I can't find out how to solve the problem. Whats wrong? When I type in 2 2, which is should work, The error message pops up and it says invalid move. But the X or O will still pop up on the board
Here's the code:
#include <iostream>
const int rows = 3;
const int elements = 3;
char SetPlayer;
char SetEnemyPlayer;
char chooseTurn = 0;
char board[rows][elements];
void Clear()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
board[i][j] = ' ';
}
}
}
void Show()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
std::cout << " " << board[i][j] << " |";
}
std::cout << std::endl;
std::cout << "------------" << std::endl;
}
}
void StartTurn()
{
std::cout << "Which character would you like to be? (X or O) "; std::cin >> chooseTurn;
switch (chooseTurn){
case 'O':
std::cout << "You have choosen O" << std::endl << std::endl;
chooseTurn = 'O';
break;
case 'X':
std::cout << "You have choosen X" << std::endl << std::endl;
chooseTurn = 'X';
break;
default:
std::cout << "Enter a valid character" << std::endl;
StartTurn();
}
}
bool PlayerAttack(int x, int y, char PlayerAttackChar)
{
if (board[x][y] == ' ')
{
board[x][y] = PlayerAttackChar;
return true;
}
return false;
}
bool EnemyAttack(int x, int y, char PlayerAttackChar)
{
if (board[x][y] == ' ')
{
board[x][y] = PlayerAttackChar;
return true;
}
return false;
}
bool OWinner()
{
if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' ||
board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' ||
board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' ||
board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' ||
board[0][2] == 'O' && board[1][2] == 'O' && board[2][0] == 'O' ||
board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' ||
board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' ||
board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
{
std::cout << "You won!" << std::endl;
return true;
}
return false;
}
bool XWinner()
{
if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' ||
board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' ||
board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' ||
board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' ||
board[0][2] == 'X' && board[1][2] == 'X' && board[2][0] == 'X' ||
board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' ||
board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' ||
board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
{
std::cout << "You won!" << std::endl;
return true;
}
return false;
}
bool error(int x, int y)
{
if (board[x][y] != ' ')
{
return true;
}
}
int main()
{
Clear();
StartTurn();
Show();
if (chooseTurn == 'X')
{
SetPlayer = 'X';
SetEnemyPlayer = 'O';
}
else
{
SetPlayer = 'O';
SetEnemyPlayer = 'X';
}
int pos1 = 0;
int pos2 = 0;
bool Xwinner = false;
bool Owinner = false;
int PlayerTurn = 0;
while (!Xwinner && !Owinner)
{
while (PlayerTurn == 0)
{
bool yourAttack = false;
PlayerTurn++;
std::cout << SetPlayer << " turn. Please input a coordinate: "; std::cin >> pos1 >> pos2; std::cout << std::endl;
pos1 -= 1;
pos2 -= 1;
if (error(pos1, pos2))
{
std::cout << "Please enter a valid position (1 to 3)" << std::endl;
PlayerTurn = 0;
}
PlayerAttack(pos1, pos2, SetPlayer);
Show();
}
while (PlayerTurn != 0)
{
PlayerTurn = 0;
std::cout<< SetEnemyPlayer << " turn. Please input a coordinate: "; std::cin >> pos1 >> pos2; std::cout << std::endl;
pos1 -= 1;
pos2 -= 1;
if (error(pos1, pos2))
{
std::cout << "Please enter a valid position (1 to 3)" << std::endl;
PlayerTurn = 1;
}
EnemyAttack(pos1, pos2, SetEnemyPlayer);
Show();
// AI
}
Xwinner = XWinner();
Owinner = OWinner();
}
}
Your error function will not return anything if the move is valid, and cause undefined behavior. You want to add a return false; after the if statement. Also you want to make sure x and y are in the bounds of the array.
bool error(int x, int y)
{
if(x > 2 || y > 2 || board[x][y] != ' ')
{
return true;
}
return false;
}
Also, you probably want you if(error) checks to be in a loop. So the game won't continue until the player enters a valid move.
while (error(pos1, pos2))
{
std::cout << "Please enter a valid position (1 to 3)" << std::endl;
PlayerTurn = 0;
}