C语言编写井字棋游戏 代码已有半成品
你初始化一个字符数组,里面都给他一样的初始值 E
X 下了就改成X ,O 下了就改成 O,下完以后判断横竖斜三条线有没有一样的,
一共就九个格子,下一步就少一步 ,下之前判断一下,如果等于E ,就是空的,可以放子。
下完一步总步数减一,你这样不就能确定还有几步可以走了。
九步都下完如果没有横竖斜都一样的不就是平局吗,所有数组元素的值都不等于E了不就结束了,或者九步完了也结束了
另外,少用GOTO ,变量定义最好有意义,写点注释
syntax error : missing ‘)’ before ‘constant’
#includetime.h
#includestdio.h
#includestdlib.h
int PvP(char a[3][3]);
int PvE(char a[3][3]);
void main()
{
int xuanze,i,j,jieguo;
char a[3][3];
for(i=0;i3;i++)
for(j=0;j3;j++)
{
a[i][j]=’_’;
}
printf(“Welcome to play Tic-Tac-Toe,Please select a mode to start the game:\n1,PvP.\n2,PvE.\n”);
scanf(“%d”,xuanze);
if(xuanze==1)
jieguo=PvP(a);
else {
printf(“Are you joking?”);
}
switch(jieguo)
{
case 1:printf(“Player 1 win!”);break;
case 2:printf(“Player 2 win!”);break;
case 3:printf(“Player win!”);break;
case 4:printf(“computer win!”);break;
case 5:printf(“Tie…”);break;
default:break;
}
printf(“\nThank you for play this game!”);
}
int PvP(char a[3][3])
{
int huihe,row,line,i,j;
for(i=0;i3;i++)
for(j=0;j3;j++)
{
printf(“%c”,a[i][j]);
}
for(huihe=1;huihe5;huihe++)
{
printf(“Player1’s turn,Please input the position of the pawn.”);
scanf(“%d %d”,row,line);
a[row][line]=’X’;
for(i=0;i3;i++)
for(j=0;j3;j++)
printf(“%c”,a[i][j]);
if((a[1][1]==’X’a[1][2]==’X’a[1][3]==’X’)
||(a[2][1]==’X’a[2][2]==’X’a[2][3]==’X’)
||(a[3][1]==’X’a[3][2]==’X’a[3][3]==’X’)
||(a[1][1]==’X’a[2][2]==’X’a[3][3]==’X’)
||(a[3][1]==’X’a[2][2]==’X’a[1][3]==’X’)
||(a[1][1]==’X’a[2][1]==’X’a[3][1]==’X’)
||(a[1][2]==’X’a[2][2]==’X’a[3][2]==’X’)//这里少了==号
||(a[1][3]==’X’a[2][3]==’X’a[3][3]==’X’))
return 1;
printf(“Player2’s turn,Please input the position of the pawn.”);
scanf(“%d %d”,row,line);
a[row][line]=’O’;
for(i=0;i3;i++)
for(j=0;j3;j++)
printf(“%c”,a[i][j]);
if((a[1][1]==’O’a[1][2]==’O’a[1][3]==’O’)
||(a[2][1]==’O’a[2][2]==’O’a[2][3]==’O’)
||(a[3][1]==’O’a[3][2]==’O’a[3][3]==’O’)
||(a[1][1]==’O’a[2][2]==’O’a[3][3]==’O’)
||(a[3][1]==’O’a[2][2]==’O’a[1][3]==’O’)
||(a[1][1]==’O’a[2][1]==’O’a[3][1]==’O’)
||(a[1][2]==’O’a[2][2]==’O’a[3][2]==’O’)//你是复制的吧,错一样的,最好不要复制,这样的习惯不好,这样会导致一大片错的,可以写函数啊,然后传入一个要判断的字母就行了
||(a[1][3]==’O’a[2][3]==’O’a[3][3]==’O’))//这里多了一个右括号
return 2;
}
}
用C语言编辑井字棋 这个程序出现了错误 但不知该怎么改 或者另做一个 谢谢了
#includestdio.h
#includectype.h
#includeconio.h
int n=9, z=0, qp[10]={0};
int chkwin(int t[], int w)
{if (t[1]==w t[1]==t[2] t[2]==t[3]) return(w);
if (t[4]==w t[4]==t[5] t[5]==t[6]) return(w);
if (t[7]==w t[7]==t[8] t[8]==t[9]) return(w);
if (t[1]==w t[1]==t[4] t[4]==t[7]) return(w);
if (t[2]==w t[2]==t[5] t[5]==t[8]) return(w);
if (t[3]==w t[3]==t[6] t[6]==t[9]) return(w);
if (t[1]==w t[1]==t[5] t[5]==t[9]) return(w);
if (t[3]==w t[3]==t[5] t[5]==t[7]) return(w);
return(0);
}
long search(int n, int k, int t[])
{int i, h, f, g;
long j;
if (n==0) return(chkwin(t,k));
for (f=0, j=0,i=1; i10; i++)
if (t[i]==0)
{t[i]=k; h=chkwin(t,k);
if (h==k) f++;
else j+=search(n-1,k*-1,t);
t[i]=0;
}
if (fz) for (j=k, g=n; g0; j*=g–);
return(j);
}
/* 电脑选位置走棋,择位原则1.当前位置必须为空 */
/* 2.若走当前位置电脑能赢,则走这一步,否则转(3) */
/* 3.若当前位置被人走电脑会输,则走这一步,否则转(4) */
/* 4.试走当前位置并调用 search()函数,求走当前位置电脑的有利程度 */
/* 5.找对电脑有利程度最大的位置走 */
void cgo()
{int i, ti=0;
long j=-8000000, t=0;
for (ti=1; ti-2; ti-=2) /*ti=1,先看自己能否赢;ti=-1,看对受能否赢*/
for (i=1; i10; i++)
if (qp[i]==0)
{qp[i]=ti;
if (chkwin(qp, ti)!=0) {n–; qp[i]=1; return;}
qp[i]=0;
}
for (i=1; i10;i++)
if (qp[i]==0)
{qp[i]=1; t=search(n-1,-1,qp);
if (tj) {j=t; ti=i;}
qp[i]=0;
}
n–; qp[ti]=1;
}
/* 函数mgo(),人输入走棋位置 */
void mgo()
{int c=0;
printf (“\nPlease enter the Num to go: “);
for (c=getche(); ; printf(“\n”), c=getche() )
if (isdigit(c) c!=’0′ qp[c-48]==0)
{n–; qp[c-48]=-1; return;
} }
/* 屏幕输出函数display,在屏幕上输出当前的棋盘 */
void display(int x)
{int i;
char t[10]={0};
for (i=1; i10; i++)
{if (qp[i]0) t[i]=88;
if (qp[i]0) t[i]=79;
}
printf (“\n%c|%c|%c\n—–\n%c|%c”, t[1], t[2], t[3], t[4], t[5]);
printf (“|%c\n—–\n%c|%c|%c\n”, t[6], t[7], t[8], t[9]);
if (x==0) printf(“\ndraw! \n”);
if (x==1) printf(“\ncomputer win!\n”);
if (x==2) printf(“\ncontinue \n”);
}
main()
{char c;
printf (“\nGo first? [Y/N]:”); /*选择谁先走*/
for (c=getche(); c!=’Y’c!=’y’c!=’N’c!=’n’; c=getche());
if (c==’N’||c==’n’) {cgo(); z=1; display(2);}
while (1)
{mgo(); if (!n) {display(0); break;} /*人走,若不是最后一步,继续;否则跳出*/
cgo(); if (chkwin(qp,1)) {display(1); break;} /*电脑走,若没赢,继续;否则跳出*/
if (n) display(2); /*还没走到最后一步,继续;否则跳出*/
else {display(0); break;}
}
getchar();
return 0;
}
VC++ 编程题 “井字棋”游戏设计(高手请进)
#include stdio.h
#include stdlib.h #define SIZE 3
typedef enum {CBLANK, CBLACK, CWHITE} CHESS;
typedef enum {GM_WIN, GM_LOST, GM_UNKNOW, GM_ERROR} GAMEFLAG;void init_board(CHESS board[SIZE][SIZE]) //初始化
{
int i, j;
for (i = 0; i SIZE; i++)
{
for (j = 0; j SIZE; j++)
{
board[i][j] = CBLANK;
}
}
}void print_chess(CHESS board[SIZE][SIZE]) //打印棋盘
{
int i, j;
putchar(‘ ‘);
for (i=0; i SIZE; i++)
{
printf(“%2d”, i+1);
}
putchar(‘\n’);
for (i=0; i SIZE; i++)
{
printf(“%-2d”, i+1);
for (j=0; j SIZE; j++)
{
switch (board[i][j])
{
case CWHITE:
putchar(‘O’);
break;
case CBLACK:
putchar(‘*’);
break;
case CBLANK:
putchar(‘_’);
break;
default:
putchar(‘?’);
break;
}
putchar(‘ ‘);
}
putchar(‘\n’);
}
}void swc(CHESS chess, int *black, int *white, int *bmax, int *wmax) //判断
{
switch (chess)
{
case CBLACK:
*white = 0;
(*black)++;
break;
case CWHITE:
*black = 0;
(*white)++;
break;
case CBLANK:
*black = 0;
*white = 0;
break;
default:
break;
} if (*black *bmax) *bmax = *black;
if (*white *wmax) *wmax = *white;
}
GAMEFLAG res(CHESS board[SIZE][SIZE]) //判断输赢
{
int i, j;
int win[4] = {0, 0, 0, 0};
int rblack, rwhite, cblack, cwhite,
loblack = 0, lowhite = 0,
roblack = 0, rowhite = 0,
bmax = 0, wmax = 0; for (i=0; i SIZE; i++)
{
rblack = 0;
rwhite = 0;
cblack = 0;
cwhite = 0; swc(board[i][i], loblack, lowhite, bmax, wmax);
swc(board[i][SIZE-i-1], roblack, rowhite, bmax, wmax); for (j=0; j SIZE; j++)
{
swc(board[i][j], rblack, rwhite, bmax, wmax);
swc(board[j][i], cblack, cwhite, bmax, wmax); } } if (bmax = 3)
{
if (wmax = 3)
{
return GM_ERROR;
}
else
{
return GM_WIN;
}
}
else
{
if (wmax = 3)
{
return GM_LOST;
}
else
{
return GM_UNKNOW;
}
}}int move(CHESS board[SIZE][SIZE], CHESS chs, int x, int y)
{
int bs = 1;
if (board[x][y])
bs = 0;
else if (y = SIZE || y 0 || x = SIZE || x 0)
bs = 0;
else
board[x][y] = chs;
return bs;
}int main()
{
CHESS b[SIZE][SIZE];
char *msg[] = {“BLACK WIN!\n”, “WHITE LOST!”, “NOT YET”, “ERROR!!”};
char *plr[] = {“NON”, “BLACK”, “WHITE”};
CHESS p = CBLACK;
GAMEFLAG flg;
init_board(b);
while ((flg = res(b)) == GM_UNKNOW)
{
int x, y, bmv = 1;
system(“cls”);
print_chess(b);
while (bmv)
{
printf(“%s回合,输入坐标:”, plr[p]);
scanf(“%d%d”, x, y);
bmv = !move(b,p,x-1,y-1);
}
p = (CHESS)(CWHITE + CBLACK – p);
}
printf(“%s”, msg[flg]);
system(“pause”);
return 0;
}
C语言 怎么编程井字棋
简单来说,定义数据结构(比如棋盘数组,棋盘格子以及棋子,双方玩家等相关的数据结构表示),定义规则(比如同一个位置不能放两颗棋子,三颗棋子连线放胜利)。
具体取决于需求,比如图形表示和AI(人工智能)等等。如果你只是想要简单的用命令行和文字输出表示,那么编写一个控制台应用程序就可以。如果需要图形等控制,需要借助一些图形以及UI库等。但是这些外部表示可以跟核心数据结构和规则,算法等分离开来。所以刚开始先用文字表示写出核心代码,后续可以逐渐加上UI图形,AI等。