Nov 10, 2015 at 10:29pm UTC
Hello. This is my first time posting on this website and I would like some help with C++ Programming. I am a beginner programmer and I need more practice. Please look over the code below. I need help with the input validation if the user inputs a letter. As of now, the code loops infinitely if I type something other than 1 or 2.
#include <iostream>
using namespace std;
char space[10] = { 'o','1','2','3','4','5','6','7','8','9' };
int status();
void board();
int gameplay();
int reset();
int main()
{
int again;
do
{
gameplay();
again = reset();
} while (again == 1);
return 0;
}
// Function to check the status of the game if Player has won, or a draw has been met
int status()
{
if (space[1] == space[2] && space[2] == space[3])
return 1;
else if (space[4] == space[5] && space[5] == space[6])
return 1;
else if (space[7] == space[8] && space[8] == space[9])
return 1;
else if (space[1] == space[4] && space[4] == space[7])
return 1;
else if (space[2] == space[5] && space[5] == space[8])
return 1;
else if (space[3] == space[6] && space[6] == space[9])
return 1;
else if (space[1] == space[5] && space[5] == space[9])
return 1;
else if (space[3] == space[5] && space[5] == space[7])
return 1;
else if (space[1] != '1' && space[2] != '2' && space[3] != '3'
&& space[4] != '4' && space[5] != '5' && space[6] != '6'
&& space[7] != '7' && space[8] != '8' && space[9] != '9')
return 0;
else
return -1;
}
// Function that creates the Tic-Tac-Toe Board
void board()
{
system("cls");
cout << " | | " << endl;
cout << " " << space[1] << " | " << space[2] << " | " << space[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << space[4] << " | " << space[5] << " | " << space[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << space[7] << " | " << space[8] << " | " << space[9] << endl;
cout << " | | " << endl << endl;
}
int gameplay()
{
int player = 1, determinant, choice;
char selection;
do
{
board();
if ((player = (player % 2)))
player = 1;
else
player = 2;
cout << "Player " << player << ", enter a number: ";
cin >> choice;
if ((selection = (player == 1)))
selection = 'X';
else
selection = 'O';
if (choice == 1 && space[1] == '1')
space[1] = selection;
else if (choice == 2 && space[2] == '2')
space[2] = selection;
else if (choice == 3 && space[3] == '3')
space[3] = selection;
else if (choice == 4 && space[4] == '4')
space[4] = selection;
else if (choice == 5 && space[5] == '5')
space[5] = selection;
else if (choice == 6 && space[6] == '6')
space[6] = selection;
else if (choice == 7 && space[7] == '7')
space[7] = selection;
else if (choice == 8 && space[8] == '8')
space[8] = selection;
else if (choice == 9 && space[9] == '9')
space[9] = selection;
else
{
cout << "The space selected is an invalid move, please press ENTER and select again.";
player--;
cin.ignore();
cin.get();
}
determinant = status();
player++;
} while (determinant == -1);
board();
if (determinant == 1)
cout << "==>\aPlayer " << --player << " is victorious! ";
else
cout << "==>\aGame draw";
cin.ignore();
cin.get();
return 0;
}
// Function to ask User(s) for another game and reset board
int reset()
{
int again;
cout << "Do you wish to play another game? (1 for Yes, 2 for No)" << endl;
cin >> again;
space[0] = { 'o' };
space[1] = { '1' };
space[2] = { '2' };
space[3] = { '3' };
space[4] = { '4' };
space[5] = { '5' };
space[6] = { '6' };
space[7] = { '7' };
space[8] = { '8' };
space[9] = { '9' };
//Input Validation for letters and numbers
while (again < 1 || again > 2)
{
cout << "Invalid! Please enter 1 or 2." << endl;
cin >> again;
}
return again;
}
Nov 11, 2015 at 12:17pm UTC
I have one for you against a computer. I will post it
Nov 11, 2015 at 12:20pm UTC
Also has time functions in it......
Dec 12, 2015 at 7:34pm UTC
Sorry, but I can not post it.
will send link