0% found this document useful (0 votes)
12 views5 pages

Tic Tac Toe

The document contains C++ code for a tic-tac-toe game. It initializes an array to represent the game board with '*' characters. It gets the number of moves from user input and then repeatedly gets player positions and marks the board with 'X' or 'O' depending on the player's turn. It checks for invalid positions or number of moves. At the end, it prints the final board state if the number of moves was valid.

Uploaded by

nnieva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Tic Tac Toe

The document contains C++ code for a tic-tac-toe game. It initializes an array to represent the game board with '*' characters. It gets the number of moves from user input and then repeatedly gets player positions and marks the board with 'X' or 'O' depending on the player's turn. It checks for invalid positions or number of moves. At the end, it prints the final board state if the number of moves was valid.

Uploaded by

nnieva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

using namespace std;

char convert(char&);
int main ()
{
int ire = 9;
int moves;
char irel;
while (ire > 0)
{cin >> irel; cout << ire; ire--;}

char tic[9];
int nah = 8;
while (nah >= 0)
{
tic[nah] = '*';
nah--;
}

cin >> moves;


int turn = 0;

while (turn < moves)


{
char move;
char x;
int t;
int y;

cout << tic[0] << tic[1] << tic[2] << endl << tic[3] << tic[4] << tic[5] << endl << tic[6] << tic[7]
<< tic[8] << endl << endl;

cin >> x;
cin >> y;
cin >> move;
if (y == 1)
{
if (move == 'A'){t = 0; tic[t] = 'O';}
else if (move == 'B'){t = 1; tic[t] = 'O';}
else if (move == 'C'){t = 2; tic[t] = 'O';}
else if (move == 'D'){t = 3; tic[t] = 'O';}
else if (move == 'E'){t = 4; tic[t] = 'O';}
else if (move == 'F'){t = 5; tic[t] = 'O';}
else if (move == 'G'){t = 6; tic[t] = 'O';}
else if (move == 'H'){t = 7; tic[t] = 'O';}
else if (move == 'I'){t = 8; tic[t] = 'O';}

}
else if (y == 2)

{
if (move == 'A'){t = 0; tic[t] = 'X';}
else if (move == 'B'){t = 1; tic[t] = 'X';}
else if (move == 'C'){t = 2; tic[t] = 'X';}
else if (move == 'D'){t = 3; tic[t] = 'X';}
else if (move == 'E'){t = 4; tic[t] = 'X';}
else if (move == 'F'){t = 5; tic[t] = 'X';}
else if (move == 'G'){t = 6; tic[t] = 'X';}
else if (move == 'H'){t = 7; tic[t] = 'X';}
else if (move == 'I'){t = 8; tic[t] = 'X';}
}

moves--;
}

char convert(char& a)
{
int A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, I = 8;
if (a == 'A'){return A;}
else if (a == 'B'){return B;}
else if (a == 'C'){return C;}
else if (a == 'D'){return D;}
else if (a == 'E'){return E;}
else if (a == 'F'){return F;}
else if (a == 'G'){return G;}
else if (a == 'H'){return H;}
else if (a == 'I'){return I;}
}

/*cin >> x, y;
if (y == 1)
{
cin >> move;
if (move == 'B'){tic[0] = 'O';}
}
else if (y == 2)
{
cin >> move;
t = convert(move);
tic[t] = 'X';
}*/

CORRECT ANSWER:

#include <iostream>
using namespace std;

char convert(char&);
int main ()
{
int ire = 9;
int moves;
char irel;
while (ire > 0)
{cin >> irel; ire--;}

char tic[9];
int nah = 8;
while (nah >= 0)
{
tic[nah] = '*';
nah--;
}

cin >> moves;


int turn = 0;
if ((moves < 0) && (moves > 9)){cout << "Invalid format"; return 0;}

while (turn < moves)


{
char move;
char x;
int t;
int y;

cin >> x;
cin >> y;
cin >> move;
if (y == 1)
{
if (move == 'A'){t = 0; tic[t] = 'O';}
else if (move == 'B'){t = 1; tic[t] = 'O';}
else if (move == 'C'){t = 2; tic[t] = 'O';}
else if (move == 'D'){t = 3; tic[t] = 'O';}
else if (move == 'E'){t = 4; tic[t] = 'O';}
else if (move == 'F'){t = 5; tic[t] = 'O';}
else if (move == 'G'){t = 6; tic[t] = 'O';}
else if (move == 'H'){t = 7; tic[t] = 'O';}
else if (move == 'I'){t = 8; tic[t] = 'O';}
else {cout << "Out of bounds"; return 0;}

}
else if (y == 2)

{
if (move == 'A'){t = 0; tic[t] = 'X';}
else if (move == 'B'){t = 1; tic[t] = 'X';}
else if (move == 'C'){t = 2; tic[t] = 'X';}
else if (move == 'D'){t = 3; tic[t] = 'X';}
else if (move == 'E'){t = 4; tic[t] = 'X';}
else if (move == 'F'){t = 5; tic[t] = 'X';}
else if (move == 'G'){t = 6; tic[t] = 'X';}
else if (move == 'H'){t = 7; tic[t] = 'X';}
else if (move == 'I'){t = 8; tic[t] = 'X';}
else {cout << "Out of bounds"; return 0;}
}
else {cout << "Invalid format"; return 0;}
moves--;
}
if (moves == 0){cout << tic[0] << tic[1] << tic[2] << endl << tic[3] <<
tic[4] << tic[5] << endl << tic[6] << tic[7] << tic[8] << endl << endl;
}
else {cout << "Invalid format"; return 0;}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy