Using visual logic build a tic tac toe game. Use the console for all input and o
ID: 3836536 • Letter: U
Question
Using visual logic build a tic tac toe game. Use the console for all input and output. Samples of what the game should look like are provided below. There are two versions of this game that you can produce. One is a user vs user mode, the other is user vs computer. Use an array to keep track of the available squares in the game board. The game will require the use of an indefinite loop that ends when someone wins or there are no more possible moves. For both versions you will require a function/procedure to print out the current state of the board, called printBoard, so that the user can choose their next move. The current state of the board will also print along with a selection guide ( as seen in the samples). You will require another procedure to take the user input, called userMove, and make appropriate changes to the array containing the board status. Use appropriate error checking to make sure that they enter a valid range and that there isn't already something in that position in the board. You will also have to keep track of who'se turn it is. This userInput procedure will require the use of another function/procedure, called checkBoard, that checks to see if the last move resulted in a win, if so the appropriate output is shown and the game is terminated. The second mode of this game is worth slightly more points. It requires some modification to the userMove() procedure and the addition of a new procedure called aiMove that make moves for player X. It is very similar to userInput, with the diference that instead of getting input from the user, the program will use the Random() function to pick a number between 1 and 9. The same validation checks should be used to ensure the square the computer randomly picks is not already taken.
Explanation / Answer
//#include "TicTacToe.h";
#include <iostream>
#include <ctime>
#include <string>
#include <ctime>
#include <cctype>
char square[10] = { '~','1','2','3','4','5','6','7','8','9' }; /* ARRAY */
int toss;
char mark;
int choice;
int checkwin();
void printBoard();
void getComputerMove();
void getPlayerMove();
//void coinToss(int player);
int winner;
int player;
int computer;
char again;
char call;
int flip;
int a = 0;
int t; // turn
//int whosOnFirst;
using namespace std;
//MAIN PROGRAM
int main()
{
//coinToss(player); // void COINTOSS
char status;
bool again = true;
status = -1;
do {
printBoard();
getPlayerMove();
status = checkwin(); winner = 1; //check & ID, player, winner
if (status != -1) // we have a winner - player
break; // leave do while
printBoard();
getComputerMove();
status = checkwin(); winner = 2; // check & ID, computer, winner
if (status != -1) // we have a winner - computer
break; // leave do while
} while (status == -1);
if (status == 0)
cout << " All right, we'll call it a draw. ";
else
if (winner == 1)
{
printBoard();
cout << " ******************";
cout << " Congradulations, ";
cout << " Player, you win!";
cout << " ****************** ";
}
else
{
printBoard();
cout << " ******************";
cout << " Computer wins. ";
cout << " ****************** ";
}
}
// FUNCTION TO RETURN GAME STATUS************************************************
// 1 = Game over with result
// -1 = Game in progress
// O = Game over no result
int checkwin()
{
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[1] == square[5] && square[5] == 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;
}
// DRAW TIC TAC TOE BOARD --------------------------------------------------------------------
void printBoard()
{
cout << " Tic Tac Toe ";
cout << " Player: X | Computer: O ";
cout << endl;
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;
}
//GET PLAYER MOVE *****************************************************************
void getPlayerMove()
{
mark = 'X';
cout << "Player 1, please enter your move: ";
cin >> choice;
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 ";
cin.ignore();
cin.get();
}
}
//GET COMPUTER MOVE ****************************************************************
void getComputerMove()
{
mark = 'O';
int c = 0;
do
{
//FTW
// FTW row 1
if (square[1] == 'O' &&
square[2] == 'O' &&
square[3] == '3')
{
square[3] = mark;
c = 1;
}
else if (square[1] == 'O' &&
square[3] == 'O' &&
square[2] == '2')
{
square[2] = mark;
c = 1;
}
else if (square[2] == 'O' &&
square[3] == 'O' &&
square[1] == '1')
{
square[1] = mark;
c = 1;
}
// FTW row 2
else if (square[4] == 'O' &&
square[5] == 'O '&&
square[6] == '6')
{
square[6] = mark;
c = 1;
}
else if (square[4] == 'O' &&
square[6] == 'O' &&
square[5] == '5')
{
square[5] = mark;
c = 1;
}
else if (square[5] == 'O' &&
square[6] == 'O' &&
square[4] == '4')
{
square[4] = mark;
c = 1;
}
// FTW row 3
else if (square[7] == 'O' &&
square[8] == 'O' &&
square[9] == '9')
{
square[9] = mark;
c = 1;
}
else if (square[7] == 'O' &&
square[9] == 'O' &&
square[8] == '8')
{
square[8] = mark;
c = 1;
}
else if (square[8] == 'O' &&
square[9] == 'O' &&
square[7] == '7')
{
square[7] = mark;
c = 1;
}
// FTW column 1
else if (square[1] == 'O' &&
square[4] == 'O' &&
square[7] == '7')
{
square[7] = mark;
c = 1;
}
else if (square[1] == 'O' &&
square[7] == 'O' &&
square[4] == '4')
{
square[4] = mark;
c = 1;
}
else if (square[4] == 'O' &&
square[7] == 'O' &&
square[1] == '1')
{
square[1] = mark;
c = 1;
}
// FTW column 2
else if (square[2] == 'O' &&
square[5] == 'O' &&
square[8] == '8')
{
square[8] = mark;
c = 1;
}
else if (square[2] == 'O' &&
square[8] == 'O' &&
square[5] == '5')
{
square[5] = mark;
c = 1;
}
else if (square[5] == 'O' &&
square[8] == 'O' &&
square[2] == '2')
{
square[2] = mark;
c = 1;
}
// FTW column 3
else if (square[3] == 'O' &&
square[6] == 'O' &&
square[9] == '9')
{
square[9] = mark;
c = 1;
}
else if (square[3] == 'O' &&
square[9] == 'O' &&
square[6] == '6')
{
square[6] = mark;
c = 1;
}
else if (square[6] == 'O' &&
square[9] == 'O' &&
square[3] == '3')
{
square[3] = mark;
c = 1;
}
// FTW m=+
else if (square[7] == 'O' &&
square[5] == 'O' &&
square[3] == '3')
{
square[3] = mark;
c = 1;
}
else if (square[5] == 'O' &&
square[3] == 'O' &&
square[7] == '7')
{
square[7] = mark;
c = 1;
};
if (square[3] == 'O' &&
square[7] == 'O' &&
square[5] == '5')
{
square[5] = mark;
c = 1;
}
// FTW m=-
else if (square[1] == 'O' &&
square[5] == 'O' &&
square[9] == '9')
{
square[9] = mark;
c = 1;
}
else if (square[1] == 'O' &&
square[9] == 'O' &&
square[5] == '5')
{
square[5] = mark;
c = 1;
}
else if (square[5] == 'O' &&
square[9] == 'O' &&
square[1] == '1')
{
square[1] = mark;
c = 1;
}
// BLOCK
// block row 1
else if (square[1] == 'X' &&
square[2] == 'X' &&
square[3] == '3')
{
square[3] = mark;
c = 1;
}
else if (square[1] == 'X' &&
square[3] == 'X' &&
square[2] == '2')
{
square[2] = mark;
c = 1;
}
else if (square[2] == 'X' &&
square[3] == 'X' &&
square[1] == '1')
{
square[1] = mark;
c = 1;
}
// block row 2
else if (square[4] == 'X' &&
square[5] == 'X' &&
square[6] == '6')
{
square[6] = mark;
c = 1;
}
else if (square[5] == 'X' &&
square[6] == 'X' &&
square[4] == '4')
{
square[4] = mark;
c = 1;
}
else if (square[6] == 'X' &&
square[4] == 'X' &&
square[5] == '5')
{
square[5] = mark;
c = 1;
}
// block row 3
else if (square[8] == 'X' &&
square[9] == 'X' &&
square[7] == '7')
{
square[7] = mark;
c = 1;
}
else if (square[7] == 'X' &&
square[9] == 'X' &&
square[8] == '8')
{
square[8] = mark;
c = 1;
}
else if (square[7] == 'X' &&
square[8] == 'X' &&
square[9] == '9')
{
square[9] = mark;
c = 1;
}
// BLOCK COLUMNS
// block COLUMN 1
else if (square[4] == 'X' &&
square[7] == 'X' &&
square[1] == '1')
{
square[1] = mark;
c = 1;
}
else if (square[1] == 'X' &&
square[7] == 'X' &&
square[4] == '4')
{
square[4] = mark;
c = 1;
}
else if (square[1] == 'X' &&
square[4] == 'X' &&
square[7] == '7')
{
square[7] = mark;
c = 1;
}
// block COLUMN 2
else if (square[5] == 'X' &&
square[8] == 'X' &&
square[2] == '2')
{
square[2] = mark;
c = 1;
}
else if (square[2] == 'X' &&
square[8] == 'X' &&
square[5] == '5')
{
square[5] = mark;
c = 1;
}
else if (square[2] == 'X' &&
square[5] == 'X' &&
square[8] == '8')
{
square[8] = mark;
c = 1;
}
// block COLUMN 3
else if (square[6] == 'X' &&
square[9] == 'X' &&
square[3] == '3')
{
square[3] = mark;
c = 1;
}
else if (square[3] == 'X' &&
square[9] == 'X' &&
square[6] == '6')
{
square[6] = mark;
c = 1;
}
else if (square[3] == 'X' &&
square[6] == 'X' &&
square[9] == '9')
{
square[9] = mark;
c = 1;
}
// BLOCK m=+
else if (square[5] == 'X' &&
square[3] == 'X' &&
square[7] == '7')
{
square[7] = mark;
c = 1;
}
else if (square[7] == 'X' &&
square[3] == 'X' &&
square[5] == '5')
{
square[5] = mark;
c = 1;
}
else if (square[7] == 'X' &&
square[5] == 'X' &&
square[3] == '3')
{
square[3] = mark;
c = 1;
}
// block m=-
else if (square[5] == 'X' &&
square[8] == 'X' &&
square[1] == '1')
{
square[1] = mark;
c = 1;
}
else if (square[1] == 'X' &&
square[8] == 'X' &&
square[5] == '5')
{
square[5] = mark;
c = 1;
}
else if (square[1] == 'X' &&
square[5] == 'X' &&
square[8] == '8')
{
square[8] = mark;
c = 1;
}
// PLAY
// play middle
else if (square[5] == '5')
{
square[5] = mark;
c = 1;
}
// play CORNERS
else if (square[1] == 'X' &&
(square[7] == '7' || square[7] == 'O') &&
(square[9] == '9' || square[9] == 'O') &&
square[3] == '3')
{
square[3] = mark;
c = 1;
}
else if (square[3] == 'X' &&
(square[1] == '1' || square[1] == 'O') &&
(square[7] == '7' || square[7] == 'O') &&
square[7] == '9')
{
square[9] = mark;
c = 1;
}
else if (square[9] == 'X' &&
(square[1] == '1' || square[1] == 'O') &&
(square[3] == '3' || square[3] == 'O') &&
square[7] == '7')
{
square[7] = mark;
c = 1;
}
else if (square[7] == 'X' &&
(square[3] == '3' || square[3] == 'O') &&
(square[9] == '9' || square[9] == 'O') &&
square[1] == '1')
{
square[1] = mark;
c = 1;
}
// play sides
else if (square[2] == 'X' &&
(square[4] == '4' || square[4] == 'O') &&
(square[8] == '8' || square[8] == 'O') &&
square[6] == '6')
{
square[6] = mark;
c = 1;
}
else if (square[6] == 'X' &&
(square[2] == '2' || square[2] == 'O') &&
(square[4] == '4' || square[4] == 'O') &&
square[8] == '8')
{
square[8] = mark;
c = 1;
}
else if (square[8] == 'X' &&
(square[2] == '2' || square[2] == 'O') &&
(square[6] == '6' || square[6] == 'O') &&
square[4] == '4')
{
square[4] = mark;
c = 1;
}
else if (square[4] == 'X' &&
(square[6] == '6' || square[6] == 'O') &&
(square[8] == '8' || square[8] == 'O') &&
square[2] == '2')
{
square[2] = mark;
c = 1;
};
} while (c = 0);
//cout << "Computer, please enter your move: ";
//cin >> choice;
// mark = '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 ";
// cin.ignore();
// cin.get();
//}
}
// VOID COIN TOSS ****************************************************************
//void coinToss(int player)
//{
// srand(time(0));
//
// do
// {
// flip = rand() % 2;// assign random numbers
// a = a++;
//
//
//
// cout << " Please choose heads or tails (h/t): ";
// cin >> call;
//
// if (flip = 0) // if TOSS HEADS
// {
// if (call == 'H' || call == 'h') // & CALL HEADS
// {
// cout << flip << ": Head's, player goes first.";
// player = 0;
// }
// else // & CALL NOT HEADS (TAILS)
// {
// cout << flip << ": Head's, computer goes first.";
// player = 1;
// }
// }
// else // & TOSS NOT HEADS (TAILS)
// {
// if (call == 'T' || call == 't') // & CALL TAILS
// {
// cout << flip << ": Tails, player goes first.";
// player = 0;
// }
// else // & NOT CALL TAILS
// {
// cout << flip << ": Tails, computer goes first.";
// player = 1;
// }
// }
// } while (a < 1);
//} // end void cointoss
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.