tictactoe: check for invalid input - c++

I'm building a tictactoe game in c++, and whenever users input a number that isn't 1-9, the output is supposed to be 'invalid move' and continue the game
else
{
cout << "Invalid move ";
player--;
cin.ignore();
cin.get();
}
but it instead ends up generating the board repeatedly, and essentially breaking the game. Here is the full code:
// This is a trial for tic tac toe in C++
#include <iostream>
using namespace std;
char *squares = new char[9]{'1', '2', '3', '4', '5', '6', '7', '8', '9'};
void play();
void getBoard();
int checkWin();
int main()
{
char *playAgain = new char;
do
{
play();
cout << "Do you want to play again(y/n): ";
cin >> *playAgain;
} while (tolower(*playAgain) == 'y');
delete squares, playAgain;
cin.get();
return 0;
}
//Play the game
void play()
{
int *i = new int;
int player = 1;
int *choice = new int;
char *mark = new char;
do
{
getBoard();
player = (player%2) ? 1 : 2;
cout << "Enter your choice: ";
cin >> *choice;
*mark = (player == 1) ? 'X' : 'O';
if (squares[0] == '1' && *choice == 1)
{
squares[0] = *mark;
}
else if (squares[1] == '2' && *choice == 2)
{
squares[1] = *mark;
}
else if (squares[2] == '3' && *choice == 3)
{
squares[2] = *mark;
}
else if (squares[3] == '4' && *choice == 4)
{
squares[3] = *mark;
}
else if (squares[4] == '5' && *choice == 5)
{
squares[4] = *mark;
}
else if (squares[5] == '6' && *choice == 6)
{
squares[5] = *mark;
}
else if (squares[6] == '7' && *choice == 7)
{
squares[6] = *mark;
}
else if (squares[7] == '8' && *choice == 8)
{
squares[7] = *mark;
}
else if (squares[8] == '9' && *choice == 9)
{
squares[8] = *mark;
}
else
{
cout << "Invalid move ";
player--;
cin.ignore();
cin.get();
}
*i = checkWin();
player++;
} while (*i == -1);
getBoard();
if (*i == 1)
{
cout << "\aPlayer " << --player << " Wins" << endl;
delete mark, choice, i;
}
else
{
cout << "\aGame Draw" << endl;
delete mark, choice, i;
}
}
// Print the board
void getBoard()
{
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X) - Player 2 (O)" << endl
<< endl;
cout << endl;
cout << " | | " << endl;
cout << " " << squares[0] << " | " << squares[1] << " | " << squares[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << squares[3] << " | " << squares[4] << " | " << squares[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << squares[6] << " | " << squares[7] << " | " << squares[8] << endl;
cout << " | | " << endl
<< endl;
}
/**********************************************************************************************************
Return 1 if some one wins
Return 0 if draw
Return -1 if the game is not over
***********************************************************************************************************/
int checkWin()
{
if (squares[0] == squares[1] && squares[1] == squares[2])
{
return 1;
}
else if (squares[0] == squares[3] && squares[3] == squares[6])
{
return 1;
}
else if (squares[0] == squares[4] && squares[4] == squares[8])
{
return 1;
}
else if (squares[3] == squares[4] && squares[4] == squares[5])
{
return 1;
}
else if (squares[1] == squares[4] && squares[4] == squares[7])
{
return 1;
}
else if (squares[6] == squares[4] && squares[4] == squares[2])
{
return 1;
}
else if (squares[6] == squares[7] && squares[7] == squares[8])
{
return 1;
}
else if (squares[2] == squares[5] && squares[5] == squares[8])
{
return 1;
}
else if (squares[0] != '1' && squares[1] != '2' && squares[2] != '3' && squares[3] != '4' && squares[4] != '5' && squares[5] != '6' && squares[6] != '7' && squares[7] != '8' && squares[8] != '9')
{
return 0;
}
else
{
return -1;
}
}
How do I resolve this?
ps: any tips on improving the game logic would also be appreciated

Related

I'm making a Tic-Tac-Toe game and I'm having a logical error for when the game runs the X's and O's can overtake each other

So, this bug has something to do with converting the player choice 1-9 to displaying it on the board with a X or O. It displays correctly it's just they can over loop each other buy just putting in the same input and I'm not quite sure how to fix this. I've tried moving the convert() around the play game() in the for loop to see if that would somehow fix it by trial and error but no it seems its a coding issue in the convert() function I just don't know where or what that issue is. I'm still learning C++ I'm just testing myself I saw a few people make a tik tac toe game online and I wanted to give it a shot, so any advice is appreciated.
#include "functions.h"
#include <iostream>
#include <vector>
using namespace std;
vector<string> player_icons = { "X", "O" };
vector<string> grid = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
int player1 = 0;
int player2 = 0;
string who_Won1 = "Player 1";
string who_Won2 = "Player 2";
void intro() {//The intro sequence to game
cout << "==================\n";
cout << " Tic Tac Toe Game \n";
cout << "==================\n";
cout << "\n";
cout << "Instructions: This game will require 2 players\n";
cout << "To win you need to match 3 in a row of the same\n";
cout << "\n";
cout << "Player 1: X\n";
cout << "Player 2: O\n";
cout << "\n";
cout << " | | \n";
cout << " 1 | 2 | 3 \n";
cout << "_____|_____|_____ \n";
cout << " | | \n";
cout << " 4 | 5 | 6 \n";
cout << "_____|_____|_____ \n";
cout << " | | \n";
cout << " 7 | 8 | 9 \n";
cout << " | | \n";
cout << "\n";
cout << "Above is the example of what the grid is going to look like when you play\n";
cout << "Each player must select a number 1-9 to put there X or O there\n";
}
void board_make() {//outputs to the terminal the actual tic tac toe board
std::cout << " | | \n";
std::cout << " " << grid[0] << " | " << grid[1] << " | " << grid[2] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << grid[3] << " | " << grid[4] << " | " << grid[5] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << grid[6] << " | " << grid[7] << " | " << grid[8] << "\n";
std::cout << " | | \n";
}
bool win_condition() {//its in the name it checks for wins
bool winner = false;
//rows
if ((grid[0] == grid[1]) && (grid[1] == grid[2]) && grid[0] != " ") {
winner = true;
}
else if ((grid[3] == grid[4]) && (grid[4] == grid[5]) && grid[3] != " ") {
winner = true;
}
else if ((grid[6] == grid[7]) && (grid[7] == grid[8]) && grid[6] != " ") {
winner = true;
}
//columns
if ((grid[0] == grid[3]) && (grid[3] == grid[6]) && grid[0] != " ") {
winner = true;
}
else if ((grid[1] == grid[4]) && (grid[4] == grid[7]) && grid[1] != " ") {
winner = true;
}
else if ((grid[2] == grid[5]) && (grid[5] == grid[8]) && grid[2] != " ") {
winner = true;
}
//across
if ((grid[0] == grid[4]) && (grid[4] == grid[8]) && grid[0] != " ") {
winner = true;
}
else if ((grid[2] == grid[4]) && (grid[4] == grid[6]) && grid[2] != " ") {
winner = true;
}
return winner;
}
void game_start() {
for (int i = 0; i < 5; i ++) {//iterates through both players turns till after the
//Player 1's turn
cout << "\n";
cout << "Player 1 its your turn please enter 1-9 to select your choice: "; cin >> player1; convert(); cout << "\n";
board_make();
win();
cout << "\n";
//Player 2's turn
cout << "\n";
cout << "Player 2 its now your turn select your choice 1-9: "; cin >> player2; convert(); cout << "\n";
board_make();
win();
cout << "\n";
}
}
void convert() {
for (int i = 0; i < 8; i++) {
//Player 1 checking
if (player1 == i + 1 && player2 != i + 1) {
grid.at(i) = "X";
}
if (player2 == i + 1 && player1 != i + 1) {
grid.at(i) = "O";
}
}
}
void win() {
if (win_condition()) {
if ((grid[0] == grid[1]) && (grid[1] == grid[2]) && grid[0] != " ") {
if (grid[0] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[0] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[3] == grid[4]) && (grid[4] == grid[5]) && grid[3] != " ") {
if (grid[3] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[3] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[6] == grid[7]) && (grid[7] == grid[8]) && grid[6] != " ") {
if (grid[6] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[6] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
//columns
if ((grid[0] == grid[3]) && (grid[3] == grid[6]) && grid[0] != " ") {
if (grid[0] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[0] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[1] == grid[4]) && (grid[4] == grid[7]) && grid[1] != " ") {
if (grid[1] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[1] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[2] == grid[5]) && (grid[5] == grid[8]) && grid[2] != " ") {
if (grid[2] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[2] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
//across
if ((grid[0] == grid[4]) && (grid[4] == grid[8]) && grid[0] != " ") {
if (grid[0] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[0] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[2] == grid[4]) && (grid[4] == grid[6]) && grid[2] != " ") {
if (grid[2] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[2] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
}
}
I think your problem is that player1 and player2 variables only store where that players last move was. So once one player makes their second move the other player can overwrite the first move.
You could fix this by checking to see if the grid contains " " before making the move.
Best of luck!
Update: I made more of my code readable or tried to, but I fixed the bug I wanted to fix here is the updated code
#include "functions.h"
#include <iostream>
#include <vector>
using namespace std;
vector<string> player_icons = { "X", "O" };
vector<string> grid = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
int player1 = 0;
int player2 = 0;
string who_Won1 = "Player 1";
string who_Won2 = "Player 2";
bool filled = false;
void intro() {//The intro sequence to game
cout << "==================\n";
cout << " Tic Tac Toe Game \n";
cout << "==================\n";
cout << "\n";
cout << "Instructions: This game will require 2 players\n";
cout << "To win you need to match 3 in a row of the same\n";
cout << "\n";
cout << "Player 1: X\n";
cout << "Player 2: O\n";
cout << "\n";
cout << " | | \n";
cout << " 1 | 2 | 3 \n";
cout << "_____|_____|_____ \n";
cout << " | | \n";
cout << " 4 | 5 | 6 \n";
cout << "_____|_____|_____ \n";
cout << " | | \n";
cout << " 7 | 8 | 9 \n";
cout << " | | \n";
cout << "\n";
cout << "Above is the example of what the grid is going to look like when you play\n";
cout << "Each player must select a numer 1-9 to put there X or O there\n";
}
void board_make() {
std::cout << " | | \n";
std::cout << " " << grid[0] << " | " << grid[1] << " | " << grid[2] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << grid[3] << " | " << grid[4] << " | " << grid[5] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << grid[6] << " | " << grid[7] << " | " << grid[8] << "\n";
std::cout << " | | \n";
}
void win_condition() {//its in the name it checks for wins
//rows
if ((grid[0] == grid[1]) && (grid[1] == grid[2]) && grid[0] != " ") {
win(0);
}
else if ((grid[3] == grid[4]) && (grid[4] == grid[5]) && grid[3] != " ") {
win(3);
}
else if ((grid[6] == grid[7]) && (grid[7] == grid[8]) && grid[6] != " ") {
win(6);
}
// columns
if ((grid[0] == grid[3]) && (grid[3] == grid[6]) && grid[0] != " ") {
win(0);
}
else if ((grid[1] == grid[4]) && (grid[4] == grid[7]) && grid[1] != " ") {
win(1);
}
else if ((grid[2] == grid[5]) && (grid[5] == grid[8]) && grid[2] != " ") {
win(2);
}
//across
if ((grid[0] == grid[4]) && (grid[4] == grid[8]) && grid[0] != " ") {
win(0);
}
else if ((grid[2] == grid[4]) && (grid[4] == grid[6]) && grid[2] != " ") {
win(2);
}
}
void game_start() {
for (int i = 0; i < 5; i ++) {//iterates through both players turns till after the
//Player 1's turn
cout << "\n";
cout << "Player 1 its your turn please enter 1-9 to select your choice: "; cin >> player1; convert(); cout << "\n";
board_make();
win_condition();
cout << "\n";
//Player 2's turn
cout << "\n";
cout << "Player 2 its now your turn select your choice 1-9: "; cin >> player2; convert(); cout << "\n";
board_make();
win_condition();
cout << "\n";
}
}
void convert() {
for (int i = 0; i < 8; i++) {
//checks if the spot is filled if it is filled it sets filled to true
if ((grid[i] == "X" || grid[i] == "O") && grid[i] != " ") {
filled = true;
}
else if (grid[i] == " "){
filled = false;
}
if (filled == true && player1 == i + 1 && grid[i] != " ") {//if filled is true and player choice is a option that was filled then it outputs whats below
cout << "Looks like that spot is already taken, please select another space\n";
}
if (filled == true && player2 == i + 1 && grid[i] != " ") {//if filled is true and player choice is a option that was filled then it outputs whats below
cout << "Looks like that spot is already taken, please select another space\n";
}
//converting the player choice 1-9 to a X or O
if (filled == false && player1 == i + 1 && player2 != i + 1) {
grid.at(i) = "X";
}
if (filled == false && player2 == i + 1 && player1 != i + 1) {
grid.at(i) = "O";
}
}
}
void win(int num) {
if (grid[num] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[num] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
}

My program keeps closing after I select Y to continue

I'm trying to make a program to play Tic Tac Toe. I have it start asking if you would like to play, but when I press Y my program doesn't start running and just closes. I'm not to sure what is causing this. Here is my code :
#include "pch.h"
#include <iostream>
using namespace std;
char square[10] = { '0','1','2','3','4','5','6','7','8','9'};
void showWelcomeMenu();
void showMenu();
int winner();
void board();
int startGame();
int quitApp();
int startGame()
{
int player = 1, i, choice;
char mark;
do
{
board();
player = (player % 2) ? 1 : 2;
cout << "Player " << player << ", enter space number: ";
cin >> choice;
mark = (player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
cout << " Invalid move ";
player--;
cin.ignore();
cin.get();
}
i = winner();
player++;
} while (i == -1);
board();
if (i == 1)
cout << "==>\aPlayer " << --player << " wins! ";
else
cout << "==>\aDraw";
cin.ignore();
cin.get();
return 0;
}
void showWelcomeMenu()
{
char answer;
bool quit = false;
cout << "Welcome to Tic Tac Toe!\n" << endl << endl;
cout << "Would you like to play? (Y/N)" << endl << endl;
cin >> answer;
if (answer == 'Y')
{
startGame();
}
else if (answer != 'Y')
{
exit(0);
}
}
int winner()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
&& square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8'
&& square[9] != '9')
return 0;
else
return -1;
}
int quitApp()
{
exit(0);
}
void board()
{
system("cls");
cout << "\n";
cout << "Tic Tac Toe";
cout << "\n";
cout << "Player 1 (X) : Player 2 (0)" << endl << endl;
cout << "\n";
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
cout << " | | " << endl << endl;
}
int main()
{
showWelcomeMenu();
quitApp();
}
Any help would be appreciated! Thank you. Sorry if this has been answered! I couldn't find it.
The code is working perfectly for me.
Please recheck it.
Maybe you are providing wrong input. like using y instead of Y (case sensitive)
To ensure that code works despite of case you can modify condition as
if (answer == 'Y' || answer == 'y')
{
startGame();
}
moreover , I'll suggest you to simplify conditions from that do while loop.
eg.
if (choice == 1 && square[1] == '1')
square[1] = mark;
should be replace by
if (choice == 1)
square[1] = mark;
same for other conditions that decide which cell to mark.
why? because choice is an integer checking it against charecter is not necessary(actually can cause bugs due to implicit type casting)

Tic Tac Toe c++ Agrid doesn't reset

I am a beginner programmer.I have built a tic tac toe game using c++. The game is working fine until the user is prompted to repeat. This where the problem is. The program doesn't loop correctly. Any help will be appreciated. Thanks.
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
char matrix[10] = {'0','1','2','3','4','5','6','7','8','9' };
void display();
int checkwin();
int restarter();
int main(){
char repeat;
do {
string playername, player1, player2;
int winner = 0;
char mark = 0;
int number = 0;
int player = 1;
char choice = 0;
cout << "Player 1 please enter your name: ";
getline(cin, player1);
cout << "Player 2 please enter your name: ";
getline(cin, player2);
while (winner == 0)
{
display();
if (player % 2)
{
playername = player1;
}
else
playername = player2;
cout << playername << " " << "Please choose a number you want" << endl;
cin >> number;
if (player % 2)
{
mark = 'X';
}
else
mark = 'O';
if (number == 1 && matrix[1] == '1')
{
matrix[1] = mark;
}
else if (number == 2 && matrix[2] == '2')
{
matrix[2] = mark;
}
else if (number == 3 && matrix[3] == '3')
{
matrix[3] = mark;
}
else if (number == 4 && matrix[4] == '4')
{
matrix[4] = mark;
}
else if (number == 5 && matrix[5] == '5')
{
matrix[5] = mark;
}
else if (number == 6 && matrix[6] == '6')
{
matrix[6] = mark;
}
else if (number == 7 && matrix[7] == '7')
{
matrix[7] = mark;
}
else if (number == 8 && matrix[8] == '8')
{
matrix[8] = mark;
}
else if (number == 9 && matrix[9] == '9')
{
matrix[9] = mark;
}
else
{
cout << "WRONG MOVE!";
player--;
cin.ignore();
cin.get();
}
winner = checkwin();
player++;
display();
if (winner == 1)
{
cout << playername << " " << "WON!" << endl;
}
else
cout << "Its a draw!" << endl;
}
cout << "do u wana repeat?" << endl;
cin >> repeat;
} while (repeat == 'Y');
system("pause");
return 0;
}
void display()
{
system("CLS");
cout << "======= Welcome to You Tic and I Tac Your Toe =======" << endl;
cout << "======= Ivan =======" << endl;
cout << "======= & =======" << endl;
cout << "======= Mostafa =======" << endl;
cout << "=====================================================" << endl;
cout << "\n" << endl;
cout << "PLAYER 1 [X] PLAYER 2 [O]" << endl;
cout << " | | " << endl;
cout << " " << matrix[1] <<" | "<<matrix[2]<<" | "<< matrix[3] << endl;
cout << "___|___|____" << endl;
cout << " | |" << endl;
cout << " " << matrix[4] <<" | "<<matrix[5]<<" | "<< matrix[6] << endl;
cout << "___|___|____" << endl;
cout << " | |" << endl;
cout << " " << matrix[7] <<" | "<<matrix[8]<<" | " << matrix[9] << endl;
cout << " | |" << endl;
}
int checkwin()
{
if (matrix[1] == matrix[2] && matrix[2] == matrix[3])
{
return 1;
}
else if (matrix[4] == matrix[5] && matrix[5] == matrix[6])
{
return 1;
}
else if (matrix[7] == matrix[8] && matrix[8] == matrix[9])
{
return 1;
}
else if (matrix[1] == matrix[4] && matrix[4] == matrix[7])
{
return 1;
}
else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
{
return 1;
}
else if (matrix[3] == matrix[6] && matrix[6] == matrix[9])
{
return 1;
}
else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
{
return 1;
}
else if (matrix[3] == matrix[5] && matrix[5] == matrix[7])
{
return 1;
}
else if (matrix[1] == matrix[5] && matrix[5] == matrix[9])
{
return 1;
}
else if (matrix[1] != '1' && matrix[2] != '2' && matrix[3] != '3' && matrix[4] != '4' && matrix[5] != '5' && matrix[6] != '6' && matrix[7] != '7' && matrix[8] != '8' && matrix[9] != '9')
return 2;
return 0;
}
The problem is that you aren't clearing your matrix after the game is won. Your matrix is a global variable, so it is created at the beginning of the program, and is not destroyed until the program stops. When you loop, all of the local variables created in the loop are destroyed, but not global variables. You need to manually clear this array. A function like this:
void clearMatrix()
{
for(int i = 0; i<10; i++)
matrix[i] = '0'+i;
}
would clear the matrix if you execute it in the appropriate place.
int main(){
char repeat;
do {
clearMatrix();
string playername, player1, player2;
int winner = 0;
char mark = 0;
...
}
If you call this clearMatrix() function at the beginning of your do-while loop, your matrix will be reset every time.
cout << "Player 1 please enter your name: ";
cin >> player1;
cout << "Player 2 please enter your name: ";
cin >> player2;
note that you can use "cin" instead of using "getline" to avoid an error when typing the names again.
also do this:
else
cout << "Its a draw!" << endl;
}
clearMatrix();
cout << "do u wanna repeat?" << endl;
cin >> repeat;
}
as you have to clear the matrix so it is a good programming practice to call the function clearMatrix(); at the bottom as defined above, while remaining inside the do-while loop.

Unexpected unqualified-id before 'while'

Trying to make a BASIC TicTacToe game using OOP C++
The errors I'm getting are:
line 74 unexpected unqualified-id before 'while' (1)
line 139 error: expected '}' at end of input (2)
line 77 error: expected unqualified-id at end of input (3)
I have no idea how those brackets could be wrong...Thank you in advance!
Here is my code:
#include <iostream>
#include <stdlib.h>
using namespace std;
class TicTacToe
{
private:
int player=1, cw , ch1, ch2; //ch= choice for rows and columns
char pick, grid[10]= {' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
public:
int checkWin()
{
if (grid[1] == grid[2] && grid[2] == grid[3])
return 1;
else if (grid[4] == grid[5] && grid[5] == grid[6])
return 1;
else if (grid[7] == grid[8] && grid[8] == grid[9])
return 1;
else if (grid[1] == grid[4] && grid[4] == grid[7])
return 1;
else if (grid[2] == grid[5] && grid[5] == grid[8])
return 1;
else if (grid[3] == grid[6] && grid[6] == grid[9])
return 1;
else if (grid[1] == grid[5] && grid[5] == grid[9])
return 1;
else if (grid[3] == grid[5] && grid[5] == grid[7])
return 1;
else if (grid[1] != '1' && grid[2] != '2' && grid[3] != '3'
&& grid[4] != '4' && grid[5] != '5' && grid[6] != '6'
&& grid[7] != '7' && grid[8] != '8' && grid[9] != '9')
return 0;
else
return -1;
}//check for winner
char mark()
{
if(player==1)
return 'X';
else
return 'O';
}
void board()
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n"; //learned that \t is to tab it in instead of using spaces
cout << "Player 1 = X Player 2 = O" << endl << endl;
cout << endl;
cout << " 1 2 3 ";
cout <<"\n";
cout << " | | " << endl;
cout << "1 " << grid[1] << " | " << grid[2] << " | " << grid[3] << endl;
cout << " | | " << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << "2 " << grid[4] << " | " << grid[5] << " | " << grid[6] << endl;
cout << " | | " << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << "3 " << grid[7] << " | " << grid[8] << " | " << grid[9] << endl;
cout << " | | " << endl;
cout << " | | " << endl << endl;
} // and for some reason this one (3)
while (i==-1) // **this one (1)**
{
if(player %2)
player==1
else
player==2
cout<< "Please enter 1-3 for row: ";
cin>> ch1;
cout<< "Please enter 1-3 for coumns: ";
cin>>ch2;
mark();
if(ch1=1 && ch2 ==1)
mark = grid[1];
else if (ch1=2 && ch2== 1)
mark = grid[2];
else if (ch1=3 && ch2== 1)
mark = grid[3];
else if (ch1=1 && ch2== 2)
mark = grid[4];
else if (ch1=2 && ch2== 2)
mark = grid[5];
else if (ch1=3 && ch2== 2)
mark = grid[6];
else if (ch1=1 && ch2== 3)
mark = grid[7];
else if (ch1=2 && ch2== 3)
mark = grid[8];
else if (ch1=3 && ch2== 3)
mark = grid[9];
else
{
cout<<" Move is invalid";
player--; //so player can retake turn
//cin.ignore(); //ignore what was input
//cin.get(); // get answers
}
cw= checkwin();
}
board();
if(i==1)
cout<<"\aPlayer "<<--player<<" win "; // a makes a beep!
else
cout<<"\aGame draw";
//cin.ignore();
//cin.get();
return 0;
};
int main()
{
cout<<" \tWelcome to TicTacToe!";
TicTacToe game;
return 0;
} // **issue with this one (2)**
You ended your board function here:
cout << " | | " << endl << endl;
} // and for some reason this one (3) <----PROBLEM is this Closing Brace
while (i==-1) // **this one (1)**
you forgot to write ; at the end of a decleration:
while(i == -1) // **this one (1)**
{
if (player % 2)
player == 1
else
AND
player == 2 //You forgot to write ;
cout << "Please enter 1-3 for row: ";
cin >> ch1;
cout << "Please enter 1-3 for coumns: ";
cin >> ch2;
this makes probems later in the code when stuff isn't found!

Why is the winning shot not showing?

I created a simple tic-tac-toe. When the player gets the winning shot (for example you got 2 X in a row and all you need is one, and you enter it), the program immediately says:
"someone won"
(I am still trying to figure out, how to say the name of the player who won.)
Without turning "3" to "X" (let's say the last number is 3).
If I enter 3, "3" should turn to "X" and leave "3 X (X X X)". Instead it only leaves "2 (X X 3)" and immediately says "someone won". How can I solve this?
This is my code:
#include <iostream>
#include <string>
using namespace std;
//board numbers
char numbers[10] = {'0','1','2','3','4','5','6','7','8','9'};
//structure of players
struct playersinfo
{
int playersnumber;
string player1name, player2name, playersturn;
char player1mark, player2mark;
};
playersinfo players;
//prototype of progress, drawboard, oneWon and twoWon
int progress();
void drawboard();
bool oneWon();
bool twoWon();
int main()
{
//assigns i to progress;
int i, j = 1;
char choice;
//assign players mark
players.player1mark = 'X';
players.player2mark = 'O';
//ask players their name
cout << "Enter player 1's name : ";
cin >> players.player1name;
system("cls");
cout << "Enter player 2's name : ";
cin >> players.player2name;
do
{
if(j == 1)
{
players.playersturn = players.player1name;
players.playersnumber = 1;
}
else if(j == 2)
{
players.playersturn = players.player2name;
players.playersnumber = 2;
}
drawboard();
cout << '\t' <<players.playersturn << " enter a number : ";
cin >> choice;
if(choice == numbers[1])
{
if(players.playersnumber == 1)
numbers[1] = players.player1mark;
else
numbers[1] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[2])
{
if(players.playersnumber == 1)
numbers[2] = players.player1mark;
else
numbers[2] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[3])
{
if(players.playersnumber == 1)
numbers[3] = players.player1mark;
else
numbers[3] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[4])
{
if(players.playersnumber == 1)
numbers[4] = players.player1mark;
else
numbers[4] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[5])
{
if(players.playersnumber == 1)
numbers[5] = players.player1mark;
else
numbers[5] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[6])
{
if(players.playersnumber == 1)
numbers[6] = players.player1mark;
else
numbers[6] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[7])
{
if(players.playersnumber == 1)
numbers[7] = players.player1mark;
else
numbers[7] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[8])
{
if(players.playersnumber == 1)
numbers[8] = players.player1mark;
else
numbers[8] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[9])
{
if(players.playersnumber == 1)
numbers[9] = players.player1mark;
else
numbers[9] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
//check progress
i = progress();
if(i == -1)
{
cout << "\n\n\t\tGame is tied up";
break;
}
}while(i != 1);
//congratulate player
if(i == 1)
{
}
cin.ignore();
cin.get();
return 0;
}
//definitions of IsTied, IsOver and drawboard
/*********************************
1 = Game is over
0 = Game is sill in progress
-1 = Game is tied up
*********************************/
int progress()
{
if(numbers[1] == numbers[2] && numbers[2] == numbers[3])
return 1;
else if(numbers[4] == numbers[5] && numbers[5] == numbers[6])
return 1;
else if(numbers[7] == numbers[8] && numbers[8] == numbers[9])
return 1;
else if(numbers[1] == numbers[4] && numbers[4] == numbers[7])
return 1;
else if(numbers[2] == numbers[5] && numbers[5] == numbers[8])
return 1;
else if(numbers[3] == numbers[6] && numbers[6] == numbers[9])
return 1;
else if(numbers[7] == numbers[5] && numbers[5] == numbers[3])
return 1;
else if(numbers[9] == numbers[5] && numbers[5] == numbers[9])
return 1;
else if(numbers[1] != '1' && numbers[2] != '2' && numbers[3] != '3'
&& numbers[4] != '4' && numbers[5] != '5' && numbers[6] != '6'
&& numbers[7] != '7' && numbers[8] != '8' && numbers[9] != '9')
return -1;
else
return 0;
}
/****************************
FUNCTION THAT DRAWS THE BOARD
****************************/
void drawboard()
{
//clear system screen
system("cls");
cout << "\n\n\t\tTic Tac Toe\n\n";
cout << '\t' <<players.player1name << " = (X) --- " << players.player2name << " = (O)" << endl << endl;
cout << '\t' << " | | " << endl;
cout << '\t' << " " << numbers[1] << " | " << numbers[2] << " | " << numbers[3] << " " << endl;
cout << '\t' << "_____|_____|_____" << endl;
cout << '\t' << " | | " << endl;
cout << '\t' << " " << numbers[4] << " | " << numbers[5] << " | " << numbers[6] << " " << endl;
cout << '\t' << "_____|_____|_____" << endl;
cout << '\t' << " | | " << endl;
cout << '\t' << " " << numbers[7] << " | " << numbers[8] << " | " << numbers[9] << " " << endl;
cout << '\t' << " | | " << endl;
}
I made a java program to do this in college, I don't know how to program it in C++ but basically, to find out who won, you get a boolean which is true when it's player 1s go and false when it's player 2s go. That way when someone wins you can make the program look at the boolean and that will tell you who made the last move.
To solve the fact it says someone won before it puts in the final X just put the code that checks if they won to run after the code that puts down the X
do
{
if(j == 1)
{
players.playersturn = players.player1name;
players.playersnumber = 1;
}
else if(j == 2)
{
players.playersturn = players.player2name;
players.playersnumber = 2;
}
drawboard(); // 4. this point is not reached with final board configuration
cout << '\t' <<players.playersturn << " enter a number : ";
cin >> choice; // 1. user makes winning move
//check progress
i = progress(); // 2. game ended (i=1)
if(i == -1)
{
cout << "\n\n\t\tGame is tied up";
break;
}
}while(i != 1); // 3. program exits this while
// dirty-fix: just a drawboard here:
drawboard();