Tic Tac Toe

niyaz

New Member
salam manbe komake c++ programmers vaghean ehtiyaj daram khahesh mikonam age kasi az in barname saer dar miyare dar asrae vaht pasokh bede
sharayet barname
1) bazi XO ba C++ bayad sakhte beshe
2)bayad kootah bashe
3)bedoon arrey
4) az loophaye sade messe FOR SWITCH WHILE ... esftefade shode bashe
age in barnamaro ghablan dashtin ya dorost kardin be man email bezanin
[email protected]
 

saalek110

Well-Known Member
بازی Tic-Tac-Toe .
تست شد.
البته شرایطی که در پست بالا گفته شده را شاید نداشته باشد. من این سورس را موقع جستجو پیدا کردم زدم اینجا.

کد:
/* Tic-Tac-Toe program by Steve Chapel [email protected]
   Uses alpha-beta pruning minimax search to play a "perfect" game.
   The alpha-beta pruning can be removed, but will increase search time.
   The heuristic and move ordering in Best_Move() can also be removed with
     an increase in search time. */

#include <stdio.h>
#include <ctype.h>

#define String_Length 80

#define Squares 9
typedef char Square_Type;
typedef Square_Type Board_Type[Squares];
const Square_Type Empty = ' ';

const int Infinity = 10;		/* Higher value than any score */
const int Maximum_Moves = Squares;	/* Maximum moves in a game */

int Total_Nodes;			/* Nodes searched with minimax */

/* Array describing the eight combinations of three squares in a row */
#define Possible_Wins 8
const int Three_in_a_Row[Possible_Wins][3] = {
    { 0, 1, 2 },
    { 3, 4, 5 },
    { 6, 7, 8 },
    { 0, 3, 6 },
    { 1, 4, 7 },
    { 2, 5, 8 },
    { 0, 4, 8 },
    { 2, 4, 6 }
};

/* Array used in heuristic formula for each move. */
const int Heuristic_Array[4][4] = {
    {     0,   -10,  -100, -1000 },
    {    10,     0,     0,     0 },
    {   100,     0,     0,     0 },
    {  1000,     0,     0,     0 }
};

/* Structure to hold a move and its heuristic */
typedef struct {
    int Square;
    int Heuristic;
} Move_Heuristic_Type;

/* Clear the board */
void Initialize(Board_Type Board) {
    int I;
    for (I = 0; I < Squares; I++)
	Board[I] = Empty;
}

/* If a player has won, return the winner. If the game is a tie,
   return 'C' (for cat). If the game is not over, return Empty. */
Square_Type Winner(Board_Type Board) {
    int I;
    for (I = 0; I < Possible_Wins; I++) {
	Square_Type Possible_Winner = Board[Three_in_a_Row[I][0]];
	if (Possible_Winner != Empty &&
	    Possible_Winner == Board[Three_in_a_Row[I][1]] &&
	    Possible_Winner == Board[Three_in_a_Row[I][2]])
	    return Possible_Winner;
    }

    for (I = 0; I < Squares; I++)
	if (Board[I] == Empty)
	    return Empty;

    return 'C';
}

/* Return the other player */
Square_Type Other(Square_Type Player) {
    return Player == 'X' ? 'O' : 'X';
}

/* Make a move on the board */
void Play(Board_Type Board, int Square, Square_Type Player) {
    Board[Square] = Player;
}

/* Print the board */
void Print(Board_Type Board) {
    int I;
    for (I = 0; I < Squares; I += 3) {
	if (I > 0)
	    printf("---+---+---\n");
	printf(" %c | %c | %c \n", Board[I], Board[I + 1], Board[I + 2]);
    }
    printf("\n");
}

/* Return a heuristic used to determine the order in which the
   children of a node are searched */
int Evaluate(Board_Type Board, Square_Type Player) {
    int I;
    int Heuristic = 0;
    for (I = 0; I < Possible_Wins; I++) {
	int J;
	int Players = 0, Others = 0;
	for (J = 0; J < 3; J++) {
	    Square_Type Piece = Board[Three_in_a_Row[I][J]];
	    if (Piece == Player)
		Players++;
	    else if (Piece == Other(Player))
		Others++;
	}
	Heuristic += Heuristic_Array[Players][Others];
    }
    return Heuristic;
}

/* Return the score of the best move found for a board
   The square to move to is returned in *Square */
int Best_Move(Board_Type Board, Square_Type Player, int *Square, int Move_Nbr,
	      int Alpha, int Beta) {
    int Best_Square = -1;
    int Moves = 0;
    int I;
    Move_Heuristic_Type Move_Heuristic[Squares];

    Total_Nodes++;

    /* Find the heuristic for each move and sort moves in descending order */
    for (I = 0; I < Squares; I++) {
	if (Board[I] == Empty) {
	    int Heuristic;
	    int J;
	    Play(Board, I, Player);
	    Heuristic = Evaluate(Board, Player);
	    Play(Board, I, Empty);
	    for (J = Moves-1; J >= 0 &&
			      Move_Heuristic[J].Heuristic < Heuristic; J--) {
		Move_Heuristic[J + 1].Heuristic = Move_Heuristic[J].Heuristic;
		Move_Heuristic[J + 1].Square = Move_Heuristic[J].Square;
	    }
	    Move_Heuristic[J + 1].Heuristic = Heuristic;
	    Move_Heuristic[J + 1].Square = I;
	    Moves++;
	}
    }

    for (I = 0; I < Moves; I++) {
	int Score;
	int Sq = Move_Heuristic[I].Square;
	Square_Type W;

	/* Make a move and get its score */
	Play(Board, Sq, Player);

	W = Winner(Board);
	if (W == 'X')
	    Score = (Maximum_Moves + 1) - Move_Nbr;
	else if (W == 'O')
	    Score = Move_Nbr - (Maximum_Moves + 1);
	else if (W == 'C')
	    Score = 0;
	else
	    Score = Best_Move(Board, Other(Player), Square, Move_Nbr + 1,
			      Alpha, Beta);

	Play(Board, Sq, Empty);

	/* Perform alpha-beta pruning */
	if (Player == 'X') {
	    if (Score >= Beta) {
		*Square = Sq;
		return Score;
	    } else if (Score > Alpha) {
		Alpha = Score;
		Best_Square = Sq;
	    }
	} else {
	    if (Score <= Alpha) {
		*Square = Sq;
		return Score;
	    } else if (Score < Beta) {
		Beta = Score;
		Best_Square = Sq;
	    }
	}
    }
    *Square = Best_Square;
    if (Player == 'X')
	return Alpha;
    else
	return Beta;
}

/* Provide an English description of the score returned by Best_Move */
void Describe(int Score) {
    if (Score < 0)
	printf("You have a guaranteed win.\n");
    else if (Score == 0)
	printf("I can guarantee a tie.\n");
    else
	printf("I have a guaranteed win by move %d.\n",
	       Maximum_Moves - Score + 1);
}

/* Have the human or the computer move */
void Move(Board_Type Board, Square_Type Player, int Move_Nbr) {
    int Square;

    if (Player == 'X') {
	Total_Nodes = 0;
	Describe(Best_Move(Board, 'X', &Square, Move_Nbr, -Infinity, Infinity));
	printf("%d nodes examined.\n", Total_Nodes);
	Play(Board, Square, 'X');
	printf("Move #%d - X moves to %d\n", Move_Nbr, Square + 1);
    } else {
	do {
	    printf("Move #%d - What is O's move? ", Move_Nbr);
	    scanf("%d", &Square);
	    Square--;
	} while (Board[Square] != ' ');
	Play(Board, Square, 'O');
    }
}

/* Play a game of tic-tac-toe */
void Game() {
    Square_Type Player;
    char Answer[String_Length];
    Board_Type Board;
    int Move_Nbr = 1;

    Initialize(Board);

    printf("\nDo you want to move first? ");
    scanf("%s", Answer);
    if (toupper(Answer[0]) == 'Y')
	Player = 'O';
    else
	Player = 'X';

    while(Winner(Board) == ' ') {
	Print(Board);
	Move(Board, Player, Move_Nbr);
	Player = Other(Player);
	Move_Nbr++;
    }
    Print(Board);

    if (Winner(Board) != 'C')
	printf("%c wins!\n", Winner(Board));
    else
	printf("It's a tie.\n");
}

int main() {
    char Answer[String_Length];

    printf("Welcome to Tic-Tac-Toe!\n\n");
    printf("Here is the board numbering:\n");
    printf(" 1 | 2 | 3\n");
    printf("---+---+---\n");
    printf(" 4 | 5 | 6\n");
    printf("---+---+---\n");
    printf(" 7 | 8 | 9\n");
    printf("\n");
    printf("Computer plays X, you play O.\n");

    do {
	Game();
	printf("\nDo you want to play again? ");
	scanf("%s", Answer);
    } while (toupper(Answer[0]) == 'Y');
}
 
آخرین ویرایش:
saalek جان
میشه یه برنامه دوز به من بدی اگه 4 در 4 باشه خیلی خیلی بهتره

فرق نداره 2 نفره باشه یا با کامپیوتر

فقط اجرا بشه

باید 4 نفرش بکنم بعد یکسری تغییرات
 

saalek110

Well-Known Member
منبع :
http://forum.p30world.com/showthread.php?t=64568

دوز.
تست شد با توربو سی 3 و 4 و نیم.

کد:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//using namespace std;
//***********************************
class dooz{
int a[3][3];
int t;
int k;
int n;
public: 
dooz();
void nobat(int*,int*);
void check(int*,int*);
void checkwin();
void show();
};
//*****************************
dooz::dooz()
{
int i,j;
k=1;
t=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=0; 
}

//*****************************
void dooz::show()
{
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++)
cout<<" ["<<a[i][j]<<"] ";
cout<<"\n\n";}
cout<<"\n\n";
}
//*****************************
void dooz::nobat(int *x,int*y)
{
int i,j,m=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(a[i][j])
m++;
if(m==9)
{
cout<<"The game doesn't have a winner.";
getch();
exit(0);
}
if(k==1)
{
cout<<"\nPlayer one:";
k*=-1;
n=1;
}
else
{
cout<<"\nPlayer two:";
k*=-1;
n=2;
}
do
{
cin>>*x>>*y;
cout<<"\n\n";
*x-=1;
*y-=1;
if(*x>2||*y>2||*x<0||*y<0)
cout<<"This place not exist please enter another place.";
}
while(*x>2||*y>2||*x<0||*y<0);
t++;
}
//***********************************
void dooz::check(int *x,int *y)
{
int b,X,Y;
b=1;
while(b)
{
if(a[*x][*y])
{
cout<<"This place is busy please enter another place:";
cin>>*x>>*y;
*x-=1;
*y-=1;
}
else
{
a[*x][*y]=n;
b=0;
}
}
}
//***********************************
void dooz::checkwin()
{
int i,j;
for(i=0;i<3;i++)
if(a[i][0]==a[i][1]&&a[i][1]==a[i][2]&&a[i][0]!=0&&a[i][1]!=0&&a[i][2]!=0)
{
cout<<"*** Number "<<a[i][0]<<" is winner. ***";
getch();
exit(0);
}
for(j=0;j<3;j++)
if(a[0][j]==a[1][j]&&a[1][j]==a[2][j]&&a[0][j]!=0&&a[1][j]!=0&&a[2][j]!=0)
{
cout<<"*** Number "<<a[0][j]<<" is winner. ***";
getch();
exit(0);
}
if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[0][0]!=0&&a[1][1]!=0&&a[2][2]!=0)
{
cout<<"*** Number "<<a[0][0]<<" is winner. ***";
getch();
exit(0);
}
if(a[0][2]==a[1][1]&&a[1][1]==a[2][0]&&a[0][2]!=0&&a[1][1]!=0&&a[2][0]!=0)
{
cout<<"*** Number "<<a[0][2]<<" is winner. ***";
getch();
exit(0);
}

}
//***********************************
int main()
{
int *x,*y,i,j;
x=new int;
y=new int;
if(!x)
{
cout<<"Allocation failure.";
exit(0);
}
if(!y)
{
cout<<"Allocation failure.";
exit(0);
}
dooz game;
while(1)
{
game.nobat(x,y);
game.check(x,y);
game.show();
game.checkwin();
}
getch();
return 0;
}
//***********************************

روش بازی اینه که بازیکن یک دو عدد میده برای نقطه کاشتن مهره اش و نفر دوم هم همین طور. سه در سه است دوز.

soheil1366:
سلام
بازی دوز همون سه به سه قطاره و یک بازه دو نفره
این بازی طوریه که هر کسی زودتر سه خانه ی جدول را که کنار هم به صورت ستونی یا سطری و یا ضربدری پر کند برنده است
در برنامه ابتدا از نفر اول دو عدد میپرسد عدد اول مختصات سطر و عدد دوم مختصات ستون
در ضمن اعداد باید بین 1 تا 3 باشد در غیر اینصورت باید دوباره دو عدد وارد شود و به همین روال برای نفر دوم ادامه دارد تا وقتی که کسی برنده شود
در ضمن ممکن است بازی برنده نداشته باشد
در برنامه از تابع clrscr استفاده نشده تا کامپایلر های GCC هم بتوانند استفاده کنند
 
آخرین ویرایش:

saalek110

Well-Known Member
منبع:
http://www.programmersheaven.com/download/32947/download.aspx
-----------------------------------------------
بازی دوز.
این سورس گرافیکی است . خیلی با کلاس. کامپیوتر در مقابل شما بازی می کند.
در توربو سی 3 روی ویندوز ایکس پی اجرا شد.
کد:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>

int board[10], turn = 1, flag = 0, player, comp;
int x1 = 220, y1 = 70, x2 = 230, y2 = 130;
int key, cp = 1, cx = 0, cy = 0;
enum { up = 72, left = 75, right = 77, down = 80} direction;
enum { yes , no } selected;

void menu();
void reset();
void go(int n);
void start_game();
void check_draw();
void draw_board();
void take_input();
void status_bar();
void player_first();
void put_X_O(char *ch,int pos);
void user_choice(int ycolor,int ncolor,char *str1,char *str2);

void main()
{
	int gd = DETECT, gm;

	initgraph(&gd,&gm,"c:\\tc\\bgi");
	reset();
	menu();

	getch();
}

void reset()
{
	int i;
	for(i=1;i<10;i++)
		board[i] = 2;
	turn = 1;
	flag = 0;
	x1 = 170, y1 = 70, x2 = 230, y2 = 130;
}


void menu()
{
	cleardevice();
	if(yesno(0))
	{
		player = 0;
		comp = 1;
		cleardevice();
		start_game();
	}
	else
	{
		player = 1;
		comp = 0;
		cleardevice();
		player_first();
	}
}

int make2()
{
	if(board[5] == 2)
		return 5;
	if(board[2] == 2)
		return 2;
	if(board[4] == 2)
		return 4;
	if(board[6] == 2)
		return 6;
	if(board[8] == 2)
		return 8;
	return 0;
}

int make4()
{
	if(board[1] == 2)
		return 1;
	if(board[3] == 2)
		return 3;
	if(board[7] == 2)
		return 7;
	if(board[9] == 2)
		return 9;
	return 0;
}

int posswin(int p)
{
// p==1 then X   p==0  then  O
	int i;
	int check_val,pos;

	if(p == 1)
		check_val = 18;
	else
		check_val = 50;

	i = 1;
	while(i<=9)//row check
	{
		if(board[i] * board[i+1] * board[i+2] == check_val)
		{
			if(board[i] == 2)
				return i;
			if(board[i+1] == 2)
				return i+1;
			if(board[i+2] == 2)
				return i+2;
		}
		i+=3;
	}

	i = 1;
	while(i<=3)//column check
	{
		if(board[i] * board[i+3] * board[i+6] == check_val)
		{
			if(board[i] == 2)
				return i;
			if(board[i+3] == 2)
				return i+3;
			if(board[i+6] == 2)
				return i+6;
		}
		i++;
	}

	if(board[1] * board[5] * board[9] == check_val)
	{
		if(board[1] == 2)
			return 1;
		if(board[5] == 2)
			return 5;
		if(board[9] == 2)
			return 9;
	}

	if(board[3] * board[5] * board[7] == check_val)
	{
		if(board[3] == 2)
			return 3;
		if(board[5] == 2)
			return 5;
		if(board[7] == 2)
			return 7;
	}
	return 0;
}

void go(int n)
{
	if(turn % 2)
		board[n] = 3;
	else
		board[n] = 5;
	turn++;
}

void player_first()
{
	check_draw();
	draw_board();
	status_bar();
	take_input();
}

void start_game()
{
	// p==1 then X   p==0  then  O
	if(posswin(comp))
	{
		go(posswin(comp));
		flag = 1;
	}
	else
	if(posswin(player))
		go(posswin(player));
	else
	if(make2())
		go(make2());
	else
		go(make4());
	draw_board();

	if(flag)
	{
		settextstyle(1,0,4);
		settextjustify(1,1);
		setcolor(128+RED);
		outtextxy(320,380,"Computer wins");
		getch();
		exit(0);
	}
	else
		player_first();
}

void check_draw()
{
	if(turn > 9)
	{
		settextstyle(1,0,4);
		settextjustify(1,1);
		setcolor(128+RED);
		outtextxy(320,380,"Game Draw");
		getch();
		exit(0);
	}
}

void put_X_O(char *ch,int pos)
{
	int m;
	int x = 170, y = 70;

	m = pos;
	if(m > 3)
	{
		while(m > 3)
		{
			y += 100;
			m -= 3;
		}
	}
	if(pos % 3 == 0)
		x += 200;
	else
	{
		pos %= 3;
		pos--;
		while(pos)
		{
			x += 100;
			pos--;
		}
	}
	settextstyle(1,0,7);
	settextjustify(1,1);
	setcolor(9);
	outtextxy(x+30,y+20,ch);
}

void draw_board()
{
	int j;

	setfillstyle(1,3);
	bar(150,50,450,350);
	setlinestyle(0,0,3);
	setcolor(4);
	rectangle(148,48,452,352);
	line(250,50,250,350);
	line(350,50,350,350);
	line(150,150,450,150);
	line(150,250,450,250);
	setcolor(9);
	rectangle(x1,y1,x2,y2);

	for(j=1;j<10;j++)
	{
		if(board[j] == 3)
			put_X_O("X",j);
		else
		if(board[j] == 5)
			put_X_O("O",j);
	}
}

void take_input()
{
	status_bar();
	while(1)
	{
		if(kbhit())
		{
			flushall();
			key = getch();
			//if status bar invoked
			switch (key)
			{
				case 13:
				if(board[cp] != 2)
					break;
				if(cp == posswin(player))
				{
					go(cp);
					draw_board();
					settextstyle(1,0,4);
					settextjustify(1,1);
					setcolor(128+RED);
					outtextxy(320,380,"Player Wins");
					getch();
					exit(0);
				}
				go(cp);
				draw_board();
				start_game();
				break;
				case 32:
					settextstyle(1,0,4);
					settextjustify(1,1);
					setcolor(128+BLUE);
					outtextxy(320,240,"Press Space bar to continue...");
					while(getch() != 32)
						continue;
					setcolor(0);
					outtextxy(320,240,"Press Space bar to continue...");
					draw_board();
					break;
				case 78:
				case 110:
					if(yesno(1))
					{
						cp = 1;
						cx = 0;
						cy = 0;
						reset();
						main();
					}
					cleardevice();
					draw_board();
					status_bar();
					break;
				case 88:
				case 120:
					if(yesno(1))
						exit(0);
					cleardevice();
					status_bar();
					draw_board();
					break;
				case up:
					if(cx > 0 )
					{
						setcolor(3);
						rectangle(x1,y1,x2,y2);
						setcolor(9);
						rectangle(x1,y1-100,x2,y2-100);
						y1=y1-100;
						y2=y2-100;
						cx=cx-1;
						cp=cp-3;
					}
					break;
				case down:
					if(cx < 2 )
					{
						setcolor(3);
						rectangle(x1,y1,x2,y2);
						setcolor(9);
						rectangle(x1,y1+100,x2,y2+100);
						y1=y1+100;
						y2=y2+100;
						cx=cx+1;
						cp=cp+3;
					}
					break;
				case left:
					if(cy > 0 )
					{
						setcolor(3);
						rectangle(x1,y1,x2,y2);
						setcolor(9);
						rectangle(x1-100,y1,x2-100,y2);
						x1=x1-100;
						x2=x2-100;
						cy=cy-1;
						cp=cp-1;
					}
					break;
				case right:
					if(cy < 2 )
					{
						setcolor(3);
						rectangle(x1,y1,x2,y2);
						setcolor(9);
						rectangle(x1+100,y1,x2+100,y2);
						x1=x1+100;
						x2=x2+100;
						cy=cy+1;
						cp=cp+1;
					}
					break;
			}
		}
	}
}

int yesno(int choice)
{
	int user_key;
	char *s1, *s2, *s3;
	selected = no;
	settextstyle(1,0,4);
	settextjustify(1,1);
	setcolor(128+BLUE);
	if(choice)
	{
		s1 = "Are you sure ?";
		s2 = "YES";
		s3 = "NO";
	}
	else
	{
		s1 = "Select your Symbol";
		s2 = "O";
		s3 = "X";
	}
	outtextxy(320,200,s1);
	user_choice(0,2,s2,s3);

	while(1)
	{
		user_key = getch();
		switch(user_key)
		{
			case 75:
			case 77:
				if(selected == no)
				{
					user_choice(2,0,s2,s3);
					selected=yes;
				}
				else
				{
					user_choice(0,2,s2,s3);
					selected=no;
				}
				break;
			case 13:
				if(selected == yes)
					return 1;
				return 0;
		}
	}
}

void user_choice(int ycolor,int ncolor,char *str1,char *str2)
{
	setfillstyle(1,ycolor);
	bar(215,240,305,280);
	outtextxy(260,260,str1);
	setfillstyle(1,ncolor);
	bar(335,240,425,280);
	outtextxy(380,260,str2);
}


void status_bar()
{
	setfillstyle(1,BLUE);
	bar(95,430,490,460);
	setcolor(GREEN);
	settextstyle(1,0,3);
	settextjustify(0,0);
	outtextxy(100,450,"N : New");
	outtextxy(220,450,"Space : Pause");
	outtextxy(400,450,"X : Exit");
	setlinestyle(0,1,4);
}

من امروز که اجرا کردم دیدم یک خط کم داره. خط قرمز:

کد:
void menu();
void reset();
void go(int n);
void start_game();
void check_draw();
void draw_board();
void take_input();
void status_bar();
void player_first();
void put_X_O(char *ch,int pos);
void user_choice(int ycolor,int ncolor,char *str1,char *str2);
[COLOR="Red"]
int yesno(int choice);[/COLOR]
 
آخرین ویرایش:
خدا کنه برسم تمومش کنم امروز
این را باید 4 در 4 کنم + اون مسیریاب را تا یه جایی جلو ببرم
توضیحات اجراییش را هم بنویسم

saalek جان اگه وقت داری بیا و این را 4 در 4 کن
اگرم نداری فدای سرت
 
in ham dooze 4 * 4 :

10 bar ham test kardam

age be moshkelli khordid begid ta halesh konam ...


**********************
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//using namespace std;
/******Ali Ahmadzadeh *************/
class TicTacTeo{
int a[4][4];
int t;
int k;
int n;
public:
TicTacTeo();
void nobat(int*,int*);
void check(int*,int*);
void checkwin();
void show();
};
//*****************************
TicTacTeo::TicTacTeo()
{
int i,j;
k=1;
t=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
a[j]=0;
}

//*****************************
void TicTacTeo::show()
{
int i,j;
for(i=0;i<4;i++){
for(j=0;j<4;j++)
cout<<" ["<<a[j]<<"] ";
cout<<"\n\n";}
cout<<"\n\n";
}
//*****************************
void TicTacTeo::nobat(int *x,int*y)
{
int i,j,m=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[j])
m++;
if(m==16)
{
cout<<"The game doesn't have a winner.";
getch();
exit(0);
}
if(k==1)
{
cout<<"\nPlayer one:";
k*=-1;
n=1;
}
else
{
cout<<"\nPlayer two:";
k*=-1;
n=2;
}
do
{
cin>>*x>>*y;
cout<<"\n\n";
*x-=1;
*y-=1;
if(*x>3||*y>3||*x<0||*y<0)
cout<<"This place not exist please enter another place.";
}
while(*x>3||*y>3||*x<0||*y<0);
t++;
}
//***********************************
void TicTacTeo::check(int *x,int *y)
{
int b,X,Y;
b=1;
while(b)
{
if(a[*x][*y])
{
cout<<"This place is busy please enter another place:";
cin>>*x>>*y;
*x-=1;
*y-=1;
}
else
{
a[*x][*y]=n;
b=0;
}
}
}
//***********************************
void TicTacTeo::checkwin()
{
int i,j;
for(i=0;i<4;i++)
if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[0]!=0&&a[1]!=0&&a[2]!=0&&a[3]!=0)
{
cout<<"*** Number "<<a[0]<<" is winner. ***";
getch();
exit(0);
}
for(j=0;j<4;j++)
if(a[0][j]==a[1][j]&&a[1][j]==a[2][j]&&a[2][j]==a[3][j]&&a[0][j]!=0&&a[1][j]!=0&&a[2][j]!=0&&a[3][j]!=0)
{
cout<<"*** Number "<<a[0][j]<<" is winner. ***";
getch();
exit(0);
}
if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[2][2]==a[3][3]&&a[0][0]!=0&&a[1][1]!=0&&a[2][2]!=0&&a[3][3]!=0)
{
cout<<"*** Number "<<a[0][0]<<" is winner. ***";
getch();
exit(0);
}
if(a[0][3]==a[1][2]&&a[1][2]==a[2][1]&&a[2][1]==a[3][0]&&a[0][3]!=0&&a[1][2]!=0&&a[2][1]!=0&&a[3][0]!=0)
{
cout<<"*** Number "<<a[0][2]<<" is winner. ***";
getch();
exit(0);
}

}
//***********************************
int main()
{
int *x,*y,i,j;
x=new int;
y=new int;
if(!x)
{
cout<<"Allocation failure.";
exit(0);
}
if(!y)
{
cout<<"Allocation failure.";
exit(0);
}
TicTacTeo game;
while(1)
{
game.nobat(x,y);
game.check(x,y);
game.show();
game.checkwin();
}
getch();
return 0;
}


امیدوارم دوستان استفاده کافی را ببرن
اگه کسی هم مثل من زیاد از سی بلد نیست -- یا این برنامه را نفهمید می تونم کرکردش را توضیح بدم ...
 
آخرین ویرایش:

جدیدترین ارسال ها

بالا