Question

In: Computer Science

- Make a 2-D array to represent a Tic-Tac-Toe game/grid. Place a couple of 'X's and...

 - Make a 2-D array to represent a Tic-Tac-Toe
    game/grid. Place a couple of 'X's and 'O's 
    in the grid and display it.
    
    Do some extra printing to make it look like
    a Tic-Tac-Toe board

we are suppose to use rows and columns so just make it simple thanks!

Solutions

Expert Solution

I used basic code you will be understand easily I don't make any thing complicated so if you like liked the answer so play a tic tock game and please please upvote Mei for the efforts I made to answer this. If any query question me in comment box

tictoctoe.cpp

#include <iostream>

#include <stdlib.h>

using namespace std;

void printBoard();

void playerInput();

void machineInput();

void refreshPage();

void resetGame();

int checkWinner();

int board[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

int player = 1;

int main() {

cout << "Tic Tac Toe\n\nTutorial:\n1.Type in the corresponding number given in the key map. \n2.If you lose, you will be first in the second round, otherwise, you will be second.\n";

cout << "\nPress any key to play again...\n";

char c;

cin >> c;

start:

refreshPage();

printBoard();

playerInput();

refreshPage();

printBoard();

//**********************************************

if (checkWinner() == 10) {

cout << endl;

cout << "***********************************\n";

cout << "* Game over! It is a tie! *\n";

cout << "***********************************\n";

cout << "\nPress any key to play again...\n";

char c;

cin >> c;

player = 1;

resetGame();

goto start;

}

if (checkWinner() == player) {

cout << endl;

cout << "***********************************\n";

cout << "* Game over! The winner is YOU! *\n";

cout << "***********************************\n";

cout << "\nPress any key to play again...\n";

char c;

cin >> c;

player = 2;

resetGame();

goto secround;

}

else if ((checkWinner() != 0) && (checkWinner() != player)) {

cout << endl;

cout << "***********************************\n";

cout << "* Game over! The winner is ME! *\n";

cout << "***********************************\n";

cout << "\nPress any key to play again...\n";

char c;

cin >> c;

player = 1;

resetGame();

goto start;

}

//**********************************************

refreshPage();

printBoard();

secround:

refreshPage();

printBoard();

machineInput();

refreshPage();

//printBoard();

//**********************************************

if (checkWinner() == 10) {

cout << endl;

cout << "***********************************\n";

cout << "* Game over! It is a tie! *\n";

cout << "***********************************\n";

cout << "\nPress any key to play again...\n";

char c;

cin >> c;

player = 1;

resetGame();

goto start;

}

if (checkWinner() == player) {

cout << endl;

cout << "***********************************\n";

cout << "* Game over! The winner is YOU! *\n";

cout << "***********************************\n";

cout << "\nPress any key to play again...\n";

char c;

cin >> c;

player = 2;

resetGame();

goto secround;

}

else if ((checkWinner() != 0) && (checkWinner() != player)) {

cout << endl;

cout << "***********************************\n";

cout << "* Game over! The winner is ME! *\n";

cout << "***********************************\n";

cout << "\nPress any key to play again...\n";

char c;

cin >> c;

player = 1;

resetGame();

goto start;

}

//**********************************************

refreshPage();

printBoard();

goto start;

return 0;

}

void resetGame() {

for (int i = 0; i < 9; i++) {

board[i] = 0;

}

}

void refreshPage() {

for (int i = 0; i < 100; i++) {

cout << "\n";

}

}

int checkWinner() {

if ((board[0] != 0) && (board[1] != 0) && (board[2] != 0) && (board[3] != 0) && (board[4] != 0) && (board[5] != 0) && (board[6] != 0) && (board[7] != 0) && (board[8] != 0)) {

return 10;

}

if ((board[0] == board[1]) && (board[1] == board[2]) && (board[0] != 0)) {

return board[0];

}

if ((board[3] == board[4]) && (board[4] == board[5]) && (board[3] != 0)) {

return board[3];

}

if ((board[6] == board[7]) && (board[7] == board[8]) && (board[6] != 0)) {

return board[6];

}

if ((board[0] == board[3]) && (board[3] == board[6]) && (board[0] != 0)) {

return board[0];

}

if ((board[1] == board[4]) && (board[4] == board[7]) && (board[1] != 0)) {

return board[1];

}

if ((board[2] == board[5]) && (board[5] == board[8]) && (board[2] != 0)) {

return board[2];

}

if ((board[0] == board[4]) && (board[4] == board[8]) && (board[0] != 0)) {

return board[0];

}

if ((board[2] == board[4]) && (board[4] == board[6]) && (board[2] != 0)) {

return board[2];

}

return 0;

}

void machineInput() {

int input = 9;

cout << "My turn. Thinking...\n";

if ((board[0] == board[1]) && (board[0] == player) && (board[2] == 0)) {

input = 2;

}

if ((board[1] == board[2]) && (board[1] == player) && (board[0] == 0)) {

input = 0;

}

if ((board[0] == board[2]) && (board[0] == player) && (board[1] == 0)) {

input = 1;

}

if ((board[3] == board[4]) && (board[3] == player) && (board[5] == 0)) {

input = 5;

}

if ((board[4] == board[5]) && (board[4] == player) && (board[3] == 0)) {

input = 3;

}

if ((board[3] == board[5]) && (board[3] == player) && (board[4] == 0)) {

input = 4;

}

if ((board[6] == board[7]) && (board[6] == player) && (board[8] == 0)) {

input = 8;

}

if ((board[7] == board[8]) && (board[7] == player) && (board[6] == 0)) {

input = 6;

}

if ((board[6] == board[8]) && (board[6] == player) && (board[7] == 0)) {

input = 7;

}

if ((board[0] == board[3]) && (board[0] == player) && (board[6] == 0)) {

input = 6;

}

if ((board[3] == board[6]) && (board[3] == player) && (board[0] == 0)) {

input = 0;

}

if ((board[0] == board[6]) && (board[0] == player) && (board[3] == 0)) {

input = 3;

}

if ((board[1] == board[4]) && (board[1] == player) && (board[7] == 0)) {

input = 7;

}

if ((board[4] == board[7]) && (board[4] == player) && (board[1] == 0)) {

input = 1;

}

if ((board[1] == board[7]) && (board[1] == player) && (board[4] == 0)) {

input = 4;

}

if ((board[2] == board[5]) && (board[2] == player) && (board[8] == 0)) {

input = 8;

}

if ((board[5] == board[8]) && (board[5] == player) && (board[2] == 0)) {

input = 2;

}

if ((board[2] == board[8]) && (board[2] == player) && (board[5] == 0)) {

input = 5;

}

if ((board[0] == board[4]) && (board[0] == player) && (board[8] == 0)) {

input = 8;

}

if ((board[4] == board[8]) && (board[4] == player) && (board[0] == 0)) {

input = 0;

}

if ((board[0] == board[8]) && (board[0] == player) && (board[4] == 0)) {

input = 4;

}

if ((board[2] == board[4]) && (board[2] == player) && (board[6] == 0)) {

input = 6;

}

if ((board[4] == board[6]) && (board[4] == player) && (board[2] == 0)) {

input = 2;

}

if ((board[2] == board[6]) && (board[2] == player) && (board[4] == 0)) {

input = 4;

}

if (input == 9) {

cout << "No solution, randomly filling in...\n";

int i;

while (input != 9) {

i = rand() % 9;

if (board[i] == 0) {

input = i;

}

}

}

if (player == 1) {

board[input] = 2;

}

else {

board[input] = 1;

}

cout << "Placed on address " << input << "\n";

printBoard();

}

void playerInput() {

int input;

ask:

input = 0;

cout << "Your turn. Enter a number.\n";

cin >> input;

if (input > 9) {

cout << "The number you entered is too big.\n";

goto ask;

}

else if (input < 1) {

cout << "The number you entered is too small.\n";

goto ask;

}

else if (board[input - 1] != 0) {

cout << "The number you entered has already filled in.\n";

goto ask;

}

else {

board[input - 1] = player;

}

}

void printBoard() {

char displayBoard[9] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };

for (int i = 0; i < 9; i++) {

switch (board[i]) {

case 0:

displayBoard[i] = ' ';

break;

case 1:

displayBoard[i] = 'O';

break;

case 2:

displayBoard[i] = 'X';

break;

};

}

cout << "Game Board Key Map\n";

cout << "************* *************\n";

cout << "* " << displayBoard[0] << " * " << displayBoard[1] << " * " << displayBoard[2] << " * * 1 * 2 * 3 *\n";

cout << "************* *************\n";

cout << "* " << displayBoard[3] << " * " << displayBoard[4] << " * " << displayBoard[5] << " * * 4 * 5 * 6 *\n";

cout << "************* *************\n";

cout << "* " << displayBoard[6] << " * " << displayBoard[7] << " * " << displayBoard[8] << " * * 7 * 8 * 9 *\n";

cout << "************* *************\n";

}


Related Solutions

How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
Use JavaScript/HTML to create a Tic Tac Toe game displaying X's O's. Score the result. Each...
Use JavaScript/HTML to create a Tic Tac Toe game displaying X's O's. Score the result. Each player takes a turn putting their mark until done or tied.
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT