0% found this document useful (0 votes)
56 views

Assignment Application

This document contains the code for a Tic Tac Toe game written for a Unix-like operating system. The code includes functions for initializing the game board, allowing player input, changing turns, checking for a winner, and drawing the updated board. The game uses a 3x3 character array to represent the board and alternates between 'X' and 'O' players. It checks each row, column and diagonal for three in a row to determine the winner before changing the current player's turn.

Uploaded by

Eric Song
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)
56 views

Assignment Application

This document contains the code for a Tic Tac Toe game written for a Unix-like operating system. The code includes functions for initializing the game board, allowing player input, changing turns, checking for a winner, and drawing the updated board. The game uses a 3x3 character array to represent the board and alternates between 'X' and 'O' players. It checks each row, column and diagonal for three in a row to determine the winner before changing the current player's turn.

Uploaded by

Eric Song
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/ 4

UNIX-like OSes & Software Tools

CSS3542

Assignment

LECTURER: Dr. Gary Loh Chee Wyai

Eric Song Jing Fa BCS 16090005


Program Code
//Unix-Like OSes' Assignment
//Tic Tac Toe

#include <iostream>

using namespace std;


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

void clear() //Clear screen function


{
cout << "\033[H\033[J" ;
}

void Draw() //Construct 3X3 lattices


{
clear();
cout << "Eric's linux project XOX" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

void Input() //Select the lattice


{
int a;
cout << "Press the number of the field: ";
cin >> a;

if (a == 1)
matrix[0][0] = player;
else if (a == 2)
matrix[0][1] = player;
else if (a == 3)
matrix[0][2] = player;
else if (a == 4)
matrix[1][0] = player;
else if (a == 5)
matrix[1][1] = player;
else if (a == 6)
matrix[1][2] = player;
else if (a == 7)
matrix[2][0] = player;
else if (a == 8)
matrix[2][1] = player;
else if (a == 9)
matrix[2][2] = player;
}
void ChangePlayer() //Changes turn of player
{
if (player == 'X')
player = 'O';
else
player = 'X';
}

char Win()
{
/* Player1's win condition */
if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
return 'X';
if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
return 'X';

if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')


return 'X';
if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
return 'X';
if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
return 'X';

if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')


return 'X';
if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
return 'X';

/* Player2's win condition */


if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
return 'O';
if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
return 'O';

if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')


return 'O';
if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
return 'O';
if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
return 'O';

if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')


return 'O';
if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
return 'O';

return '/';
}
int main()
{
Draw();
while (1)
{
Input();
Draw();
if (Win() == 'X')
{
cout << "Player 1 wins! GAMEOVER " << endl;
break;
}
else if (Win() == 'O')
{
cout << "Player 2 wins! GAMEOVER " << endl;
break;
}
ChangePlayer();
}

return 0;
}

You might also like