السلام عليكم ورحمة الله وبركاته
تصميم لعبة tic-toc-toe كانت احد التطبيقات في الفصل الدراسي السابق
و قد شيبتنا حتى تم انجازها و اليكم الكود باستخدام c++ وحتى تعمل اللعبة يجب ادخال
الموقع المراد وضع فيه x مثلا 1 1 تعني فى الوسط ويمكن بواسطة هذه اللعبة ان تلعب مع شخص اخر او مع الكمبيوتر
/*********************************************************************************************************
subject :c++ programming
program : Tic Tac Toe Game
This program gives the possibility for two players to play against each other or one player
against a computer . You can choose by answering the question "1/2 player(s)? .
In this program 4 states are defined,
state 0: Player won the game
state 1: computer won the game
state 2: Cat game
state 4: Game not finished
*********************************************************************************************************/
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
void clrscr()
{
COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
DWORD dwConSize; /* number of character cells in the current buffer */
/* get the output console handle */
HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
/* get the number of character cells in the current buffer */
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
/* fill the entire screen with blanks */
FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten);
/* get the current text attribute */
GetConsoleScreenBufferInfo(hConsole, &csbi);
/* now set the buffer's attributes accordingly */
FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);
/* put the cursor at (0, 0) */
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}
class TicTacToe
{
private:
char board[3][3];
char lastchar;
public:
TicTacToe();
~TicTacToe() {}
int playerSet(int x, int y);
int computerSet();
int state();
void drawBoard();
};
//-----------------------------------------
TicTacToe::TicTacToe() //constructor
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
board[j]=' ';
}
}
lastchar='o';
}
//------------------------------------------
int TicTacToe::playerSet(int x, int y)
{
if((x<0)||(x>2)||(y<0)||(y>2)) return 0;
if(board[x][y]!=' ') return 0;
if(lastchar=='x') lastchar='o';
else lastchar='x';
board[x][y]=lastchar;
return 1;
}
//-------------------------------------------
int TicTacToe::computerSet()
{
if((state()!=3)||(lastchar=='o')) return 0; // game over or not computer's turn
lastchar='o';
int i,j,o,x,e,s;
// computer tries to win the game
// checking 2 'o's in rows
for(i=0;i<3;i++)
{
o=0; s=-1;
for(j=0;j<3;j++)
{
if(board[j]=='o') o++;
if(board[j]==' ') s=j;
}
if((o==2)&&(s!=-1))
{
board='o';
return 1;
}
}
// checking 2 'o's in columns
for(j=0;j<3;j++)
{
o=0; s=-1;
for(i=0;i<3;i++)
{
if(board[j]=='o') o++;
if(board[j]==' ') s=i;
}
if((o==2)&&(s!=-1))
{
board[j]='o';
return 1;
}
}
// checking 2 'o's in diagonal 1
o=0; s=-1;
for(i=0;i<3;i++)
{
if(board=='o') o++;
if(board==' ') s=i;
}
if((o==2)&&(s!=-1))
{
board='o';
return 1;
}
// checking 2 'o's in diagonal 2
o=0; s=-1;
for(i=0;i<3;i++)
{
if(board[2-i]=='o') o++;
if(board[2-i]==' ') s=i;
}
if((o==2)&&(s!=-1))
{
board[2-s]='o';
return 1;
}
//------------------------------------------------------
// computer tries to stop the player in winning the game
// checking 2 'x' in rows
for(i=0;i<3;i++)
{
x=0; s=-1;
for(j=0;j<3;j++)
{
if(board[j]=='x') x++;
if(board[j]==' ') s=j;
}
if((x==2)&&(s!=-1))
{
board='o';
return 1;
}
}
// checking 2 'x's in columns
for(j=0;j<3;j++)
{
x=0; s=-1;
for(i=0;i<3;i++)
{
if(board[j]=='x') x++;
if(board[j]==' ') s=i;
}
if((x==2)&&(s!=-1))
{
board[j]='o';
return 1;
}
}
// checking 2 'o's in diagonal 1
x=0; s=-1;
for(i=0;i<3;i++)
{
if(board=='x') x++;
if(board==' ') s=i;
}
if((x==2)&&(s!=-1))
{
board='o';
return 1;
}
// checking 2 'o's in diagonal 2
x=0; s=-1;
for(i=0;i<3;i++)
{
if(board[2-i]=='x') x++;
if(board[2-i]==' ') s=i;
}
if((x==2)&&(s!=-1))
{
board[2-s]='o';
return 1;
}
//------------------------------------------------------
// computer tries to find a good position
// checking 1 'o' & 2 spaces in rows
for(i=0;i<3;i++)
{
o=e=0; s=-1;
for(j=0;j<3;j++)
{
if(board[j]=='o') o++;
if(board[j]==' ') {s=j; e++;}
}
if((o==1)&&(e==2))
{
board='o';
return 1;
}
}
// checking 1 'o' & 2 spaces in columns
for(j=0;j<3;j++)
{
o=e=0; s=-1;
for(i=0;i<3;i++)
{
if(board[j]=='o') o++;
if(board[j]==' ') {s=i; e++;}
}
if((o==1)&&(e==2))
{
board[j]='o';
return 1;
}
}
// checking 1 'o' & 2 spaces in diagonal 1
o=e=0; s=-1;
for(i=0;i<3;i++)
{
if(board=='o') o++;
if(board==' ') {s=i; e++;}
}
if((o==1)&&(e==2))
{
board='o';
return 1;
}
// checking 1 'o' & 2 spaces in diagonal 2
o=e=0; s=-1;
for(i=0;i<3;i++)
{
if(board[2-i]=='o') o++;
if(board[2-i]==' ') {s=i; e++;}
}
if((o==1)&&(e==2))
{
board[2-s]='o';
return 1;
}
// finding an empty place
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(board[j]==' ')
{
board[j]='o';
return 1;
}
}
}
// nowhere to set
return 0;
}
//----------------------------------------------
int TicTacToe::state()
{
int i,j,x,o;
// checking rows
for(i=0;i<3;i++)
{
x=o=0;
for(j=0;j<3;j++)
{
if(board[j]=='o') o++;
if(board[j]=='x') x++;
}
if(x==3) return 0; // player 'x' won the game
if(o==3) return 1; // player 'o' won the game
}
// checking columns
for(j=0;j<3;j++)
{
x=o=0;
for(i=0;i<3;i++)
{
if(board[j]=='o') o++;
if(board[j]=='x') x++;
}
if(x==3) return 0;
if(o==3) return 1;
}
// checking 1. diagonal
x=o=0;
for(i=0;i<3;i++)
{
if(board=='o') o++;
if(board=='x') x++;
}
if(x==3) return 0;
if(o==3) return 1;
// checking 2. diagonal
x=o=0;
for(i=0;i<3;i++)
{
if(board[2-i]=='o') o++;
if(board[2-i]=='x') x++;
}
if(x==3) return 0;
if(o==3) return 1;
// finding an empty place
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(board[j]==' ')
{
return 3; // not finished
}
}
}
// cat game (board is full)
return 2;
}
void TicTacToe::drawBoard()
{
clrscr();
cout << " ----------- \n";
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout <<" |"<<board[j]<<' ';
}
cout <<"|\n";
cout << " ----------- \n";
}
}
//--------------------------------------------
// main program
void main()
{
TicTacToe g;
int numberofplayers, player=0, i, j;
do
{
cout << "1/2 player(s)? ";
cin >> numberofplayers;
} while((numberofplayers<1)||(numberofplayers>2));
g.drawBoard();
while(g.state()==3)
{
if((numberofplayers==1)&&(player==1))
{
cout <<"\n\n"<<"Computer's turn\n\n";
cout << "\n\n\nPress any key!\n"; //getch();
g.computerSet();
}
else
{
cout <<"\n\n"<< player+1 <<". player's turn\n\n";
do
{
cout << "Give the position (i j)!"; cin >> i >> j;
}while(g.playerSet(i,j)==0);
}
g.drawBoard();
player=(player+1)%2;
}
switch(g.state())
{
case 0: cout << "Player won the game!\n"; break;
case 1: cout << "Computer won the game!\n"; break;
case 2: cout << "Cat game!\n"; break;
case 3: cout << "Not finished!\n"; break;
}
}