0% found this document useful (0 votes)
99 views3 pages

Tictactoe C++ Code

This C++ program implements a tic-tac-toe game. It initializes a 3x3 board and functions to display the board, get player input, update the board based on the input, and print the updated board. The main function calls these functions, first getting input from player 1 and updating the board, then getting input from player 2 and updating again, before ending the game.

Uploaded by

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

Tictactoe C++ Code

This C++ program implements a tic-tac-toe game. It initializes a 3x3 board and functions to display the board, get player input, update the board based on the input, and print the updated board. The main function calls these functions, first getting input from player 1 and updating the board, then getting input from player 2 and updating again, before ending the game.

Uploaded by

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

#include <iostream>

using namespace std;

void DisplayBoard(char array[][3], int size);


void GrabInput(char player);
void UpdateBoard(char array[][3], int size, char player, char currentMove);
int PrintBoard(char array[][3], int size, char currentMove);

char board[3][3] = { {'1','2','3' }, {'4','5','6'}, {'7','8','9'} };

char player1, player2;


char firstMove = 0;
char currentMove;
int currentPlayer;

int main()
{
char* pMove = &currentMove;
*pMove = currentMove;

DisplayBoard(board, 3);
GrabInput('1');
cout<<*pMove<<endl;
system("pause");
UpdateBoard(board, 3, '1', *pMove);
PrintBoard(board, 3, *pMove);
GrabInput(2);
UpdateBoard(board, 3, 2, currentMove);
PrintBoard(board, 3, currentMove);

system("pause");
return 0;
}

void DisplayBoard(char array[][3], int size)


{
system("cls");
cout<<"::Tic Tac Toe::\n\n";

for (int x = 0; x < size; x++)


{
for (int y = 0; y < size; y++)
{
cout<<array[x][y];
cout<<" ";
}
cout<<endl;
}
cout<<endl;
}

void GrabInput(char player)


{
char* pMove = &currentMove;

if (player == '1')
{
cout<<"\n";
cout<<"Your Move:\n\n";
cout<<"Player 1 (1-9): ";
cin>>player1;
//currentPlayer = player1;
*pMove = currentMove = player1;
}
if (player == '2')
{
cout<<"\n";
cout<<"Your Move:\n\n";
cout<<"Player 2 (1-9): ";
cin>>player2;
//currentPlayer = player2;
*pMove = currentMove = player2;
}
}

void UpdateBoard(char array[][3], int size, char player, char currentMove)


{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
switch(player1)
{
case '1': array[0][0] = 'X'; break;
case '2': array[1][0] = 'X'; break;
case '3': array[2][0] = 'X'; break;
case '4': array[1][0] = 'X'; break;
case '5': array[1][1] = 'X'; break;
case '6': array[1][2] = 'X'; break;
case '7': array[2][0] = 'X'; break;
case '8': array[2][1] = 'X'; break;
case '9': array[2][2] = 'X'; break;
}
switch(player2)
{
case '1': array[0][0] = 'O'; break;
case '2': array[1][0] = 'O'; break;
case '3': array[2][0] = 'O'; break;
case '4': array[1][0] = 'O'; break;
case '5': array[1][1] = 'O'; break;
case '6': array[1][2] = 'O'; break;
case '7': array[2][0] = 'O'; break;
case '8': array[2][1] = 'O'; break;
case '9': array[2][2] = 'O'; break;
}
}
}
}

int PrintBoard(char array[][3], int size, char currentMove)


{
system("cls");
cout<<"::Tic Tac Toe::\n\n";

for (int x = 0; x < size; x++)


{
for (int y = 0; y < size; y++)
{
if (currentMove == array[x][y])
{
return x;

}
cout<<endl;
}
cout<<endl;
}

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