// Program create on visual studio consol application.
// Project: create Tic Tac game.
// university: Govt. College university.
// Department of computer science.
// submitted by: Muhammad Ateek.
// Roll number: 03-bscs-2011
// sysmester: 1st
#include <iostream> // directory for input and output
#include <cstdlib> // directory to generate random numbers.
#include <time.h> // directory to generate seconds as a seeds of random number
#include <windows.h> // directory for clr screeen. and sounds.
#include <string> // directory to get string type names of players.
#include <conio.h>
void computer();
int checkWinner();
void drawBoard();
using namespace std;
int option, test, row, col,turn, with, option2 =0;
string player1,player2;
char board[3][3];
int main()
{
cout << " //////////////////////////////" << endl;
cout <<" /// TIC TAC TOE ///" << endl;
cout<<" //////////////////////////////" << endl;
do
{
option2 = 0; //so we can play multiple times
system("Color 3E"); // colour of. main game
cout<<"\nEnter a menu option:\n";
cout<<"1. Play Tic Tac Toe\n"; // first line display
cout<<"2. Game detail\n";
cout<<"3. Owner detail\n"; // sencond line display.
cout << "4. Quit"; // quit game
cin >> option; // choice to play game or owner detail or quit.
PlaySound("Sound Effect - African Drums.WAV", NULL, SND_ASYNC);
// sound play during game.
if(option==2)
{
system("cls");
cout << "\n Game detail\n"
"* Press corresponding number to any choice\n"
"* For example 1: for play game\n"
"* when enter to the game the box will be\n"
" display in this from\n"
"* frist is euals to 0\n"
"* second is equals to 1\n"
"* third is equals to 2\n"
"* same with coloums\n\n"
"----------\n"
"|00|01|02|\n"
"----------\n"
"|10|11|12|\n"
"----------\n"
"|20|21|22|\n"
"----------\n"
"\n* you have to enter the correct position\n\n";
continue;
}
if(option==3)
{ system("cls");
cout <<"\n Owner detail\nProgram create on visual studio consol application.\n"
"Project: create Tic Tac game.\n"
"university: Govt. College university.\n"
"Department of computer science.\n"
"submitted by: Muhammad Ateek.\n"
"Roll number: 03-bscs-2011\n"
"sysmester: 1st\n";
continue;
}
if(option==1) // check option if it is play.
{
cout << "1. for player VS player\n2. for player VS computer";
cin >> with; // choice to play with computer or not.
cout << "1st player = "; // first player name.
Beep(1000,600); // Beep on enter name of 1st player.
cin >> player1; // enter name.
if(with==1) // check if choice to play with another player.
{
cout << "\n2nd player = ";
Beep(1000,600); // Beep on entery second name.
cin >> player2; //entery of 2nd player.
}
system("Color 5E"); // colour after menu and start game.
drawBoard(); // draw complete frame.
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
if(option !=4)
{
while(option2 != -1)
{
if(turn%2 == 0)//if it's even then it's X's turn
{
bool condition = true; // bool to check whether the move of player is correct.
while(condition == true)
{
cout<<"\n\nEnter a move: ";
cout << player1; cout << endl; // display name of player1
cout<<"Enter the row you wish to make your move on (0,1,2)\n";
cin>>row;
cout<<"Enter the column you wish to make your move on (0,1,2)\n";
cin>>col; system("cls");
if(board[row][col] == ' ')
{
board[row][col] = 'X';
turn++;
condition = false;
}
else
{
cout<<"Invalid move!\n";
Beep(500,600);// Beep on display frame or drawboard
drawBoard();
}
}
}
else if(with==1) // check whether the game is 2 player or computer player.
{
if(turn%2 == 1)//if it's even then it's X's turn
{
bool condition = true; // bool to check correct position.
while(condition == true)
{
cout<<"\n\nEnter a move: ";
cout << player2; cout << endl; // display of second name.
cout<<"Enter the row you wish to make your move on (0,1,2)\n";
cin>>row;
cout<<"Enter the column you wish to make your move on (0,1,2)\n";
cin>>col; system("cls");
if(board[row][col] == ' ') // check if space in enter position.
{
board[row][col] = 'O';
turn++;
condition = false;
}
else
{
cout<<"Invalid move!\n";
drawBoard();
}
}
}
}
else if(with==2)
{
computer();
turn++;
}
if (checkWinner() == 1) //X wins
{
system("cls");
system("Color 4D"); PlaySound("Sound Effect - Fan Fair 01.WAV", NULL, SND_ASYNC);
cout<<"Congratulations! "; cout << player1; cout <<" won!\n\n"; with++;
option2 = -1;
}
else if(checkWinner() == 0) //X wins
{
system("cls");
system("Color 4D"); PlaySound("Sound Effect - Fan Fair 01.WAV", NULL, SND_ASYNC);
cout<<"Congratulations! ";
if(with==2)
{
cout <<"computer ";
}
else
cout<<player2;
cout<< " won!\n\n";
option2 = -1;
}
drawBoard();
}
}
}while(option != 4);
cout<<"\n\n\n\nEnter anything to quit.";
cin>>test;
}
//print the board to the screen
void drawBoard()
{
cout<<"\n";
cout<<"\n-------------\n";
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout<<"|"<<board[i][j]<<"| ";
}
cout<<"\n-------------\n";
}Beep(1000,600);
}
//this method returns a 0 if there's no winner, 1 if X wins, 2 if O wins, 3 if tie
int checkWinner()
{
//check to see if X won
if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
return 1;
if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
return 1;
if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
return 1;
if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
return 1;
if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
return 1;
if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
return 1;
if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
return 1;
if(board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')
return 1;
//check to see if O won
if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
return 0;
if(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
return 0;
if(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
return 0;
if(board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
return 0;
if(board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
return 0;
if(board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
return 0;
if(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
return 0;
if(board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O')
return 0;
}
//computer. generate a random move and see if it's available. if it is, then make it
//if it isn't then generate another random move
void computer()
{
col = -1;
row = -1;
while(col == -1 || row == -1)
{
srand( (unsigned)time(NULL) );
col = rand()%3;
row = rand()%3;
if(board[row][col] != ' ')
{
col = -1;
row = -1;
}
board[row][col] = 'O';
}
}
Thursday, 24 January 2013
Tic Tac
08:29
ATEEK
No comments
0 comments:
Post a Comment