1.c ?棋c棋?????Դ??
2.用C++编写的小游戏源代码
3.求助高手,求一个C语言五子棋的源码源代代码 带流程图的!
4.五子棋C语言代码
c ?棋c棋?????Դ??
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define TEXTS 7
#define CURSOR
#define CHESSBOARD
#define WHITECHESS
#define SELECTEDWHITE
#define BLACKCHESS
#define SELECTEDBLACK
#define qx1_num
#define qx2_num
typedef struct node
{
int step;
int color;
} NODE;
typedef struct point
{
int x;
int y;
}_POINT;
typedef struct qixing
{
char qx[8];
int value;
}QIXING;
HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
_POINT cursor;
int direction[8][2]={ { 0,-1},{ 0,1},{ -1,0},{ 1,0},{ -1,-1},{ 1,1},{ -1,1},{ 1,-1}};
QIXING qx1[qx1_num]={ { "x",},{ "1x",},{ "x",},{ "0x",},{ "x",},{ "x",},
{ "0x",},{ "x",},{ "0x",},{ "0x",},{ "x",},
{ "x1",},{ "1x",},{ "x",},{ "x",},{ "1x",},
{ "x",},{ "0x",},{ "x",},{ "0x",},{ "0x",},
{ "0x",},{ "x",},{ "x2",},{ "1x",},{ "0x",},{ "0x",5}};
QIXING qx2[qx2_num]={ { "x",},{ "1x",},{ "x",},{ "0x",},{ "x",},{ "x",},
{ "0x",},{ "x",},{ "0x",},{ "0x",},{ "x",},
{ "x1",},{ "1x",},{ "x",},{ "1x",},{ "x",},
{ "0x",},{ "x",},{ "0x",},{ "0x",},
{ "0x",},{ "x",},{ "x2",},{ "1x",},{ "0x",},{ "0x",}};
//------------------------------------------------------------------------------------------------------
void textcolor(int color)
{
SetConsoleTextAttribute(hOutput, color);
}
void gotoxy(int x, int y)
{
COORD coordScreen={ 0,0};
coordScreen.X=x;
coordScreen.Y=y;
SetConsoleCursorPosition(hOutput,coordScreen);
}
void printnode(NODE chessboard[][], int x, int y)
{
textcolor(CHESSBOARD);
if(chessboard[x][y].step==0)
{
if(x==cursor.x&&y==cursor.y)
textcolor(CURSOR);
switch(x)
{
case 0:
if(y==0)printf("┏");
else if(y==)printf("┓");
else printf("┳");
break;
case 3:
if(y==0)printf("┣");
else if(y==3||y==)printf("╬");
else if(y==)printf("┫");
else printf("╋");
break;
case 7:
if(y==0)printf("┣");
else if(y==7)printf("╬");
else if(y==)printf("┫");
else printf("╋");
break;
case :
if(y==0)printf("┣");
else if(y==3||y==)printf("╬");
else if(y==)printf("┫");
else printf("╋");
break;
case :
if(y==0)printf("┗");
else if(y==)printf("┛");
else printf("┻");
break;
default:
if(y==0)printf("┣");
else if(y==)printf("┫");
else printf("╋");
}
}
else if(chessboard[x][y].color==0)
{
if(x==cursor.x&&y==cursor.y)
textcolor(SELECTEDWHITE);
else textcolor(WHITECHESS);
printf("●");
}
else
{
if(x==cursor.x&&y==cursor.y)
textcolor(SELECTEDBLACK);
else textcolor(BLACKCHESS);
printf("●");
}
}
void printchessboard(NODE chessboard[][])
{
int i,j;
char letter[]={ " A B C D E F G H I J K L M N O\n"};
for(i=0;i<;i++)
{
textcolor(TEXTS);
printf("%2d",-i);
for(j=0;j<;j++)
printnode(chessboard,i,j);
textcolor(TEXTS);
printf("\n");
}
textcolor(TEXTS);
printf("%s",letter);
printf("移动:方向键 下棋:ENTER 悔棋:U 退出:F");
}
void renew(NODE chessboard[][],int x,int y)
{
COORD coordScreen;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(x<0||x>||y<0||y>)
return;
if(!GetConsoleScreenBufferInfo(hOutput,&csbi))
return;
coordScreen=csbi.dwCursorPosition;
gotoxy((y-1)*2+4,x+1);
printnode(chessboard,x,y);
SetConsoleCursorPosition(hOutput,coordScreen);
}
void showmenu()
{
textcolor(TEXTS);
system("cls");
printf("1.人机对战\n2.双人对战\n3.退出\n\n请选择[1~3]:");
}
void showsubmenu()
{
textcolor(TEXTS);
system("cls");
printf("1.你先手\n2.电脑先手\n3.返回上级菜单\n\n请选择[1~3]:");
}
int getchoose(int min, int max)
{
int choose;
do
{
choose=getch()-;
}while(choose<min||choose>max);
printf("%d",choose);
return choose;
}
//-------------------------------------------------------------------------------------------------
bool quit;
bool regret;
bool getmove(NODE chessboard[][])
{
char c;
for(;;)
{
c=getch();
if(c==-)
switch(getch())
{
case :
cursor.x--;
if(cursor.x<0)cursor.x=0;
renew(chessboard,cursor.x+1,cursor.y);
renew(chessboard,cursor.x,cursor.y);
break;
case :
cursor.x++;
if(cursor.x>)
cursor.x=;
renew(chessboard,cursor.x-1,cursor.y);
renew(chessboard,cursor.x,cursor.y);
break;
case :
cursor.y--;
if(cursor.y<0)cursor.y=0;
renew(chessboard,cursor.x,cursor.y+1);
renew(chessboard,cursor.x,cursor.y);
break;
case :
cursor.y++;
if(cursor.y>)cursor.y=;
renew(chessboard,cursor.x,cursor.y-1);
renew(chessboard,cursor.x,cursor.y);
break;
case :
quit=true;
return true;
}
else if(c==&&chessboard[cursor.x][cursor.y].step==0)
return true;
else if(c=='U'||c=='u')
{
regret=true;
return true;
}
}
}
void beback(NODE chessboard[][], int step)
{
int i,j,tempx,tempy;
if(step==1)return;
if(step>2)
{
for(i=0;i<;i++)
for(j=0;j<;j++)
{
if(chessboard[i][j].step==step-1)
{
chessboard[i][j].step=0;
renew(chessboard,i,j);
}
else if(chessboard[i][j].step==step-2)
{
chessboard[i][j].step=0;
tempx=cursor.x;
tempy=cursor.y;
cursor.x=i;
cursor.y=j;
renew(chessboard,i,j);
renew(chessboard,tempx,tempy);
}
}
}
else if(step==2)
{
for(i=0;i<;i++)
for(j=0;j<;j++)
if(chessboard[i][j].step==step-1)
{
chessboard[i][j].step=0;
renew(chessboard,i,j);
}
tempx=cursor.x;
tempy=cursor.y;
cursor.x=7;
cursor.y=7;
renew(chessboard,i,j);
renew(chessboard,tempx,tempy);
}
}
//-----------------------------------------------------------------------------------------
bool inside(int x,int y)
{
if(x<0||x>||y<0||y>)return false;
return true;
}
int line(NODE chessboard[][],int dirt,int x,int y,int color)
{
int i;
for(i=0;chessboard[x+direction[dirt][0]][y+direction[dirt][1]].step>0&&
chessboard[x+direction[dirt][0]][y+direction[dirt][1]].color==color;i++)
{
x=x+direction[dirt][0];
y=y+direction[dirt][1];
if(!inside(x,y))return i;
}
return i;
}
bool win(NODE chessboard[][],int x,int y,int color)
{
if(line(chessboard,0,x,y,color)+line(chessboard,1,x,y,color)>3)
return true;
if(line(chessboard,2,x,y,color)+line(chessboard,3,x,y,color)>3)
return true;
if(line(chessboard,4,x,y,color)+line(chessboard,5,x,y,color)>3)
return true;
if(line(chessboard,6,x,y,color)+line(chessboard,7,x,y,color)>3)
return true;
return false;
}
//----------------------------------------------------------------------------------------------
int attacktrend,defenttrend;
bool macth1(NODE chessboard[][],int x,int y,int dirt,int kind,int color)
{
int k;
char c;
char *p;
p=strchr(qx1[kind].qx,'x');
for(k=0;k<=p-qx1[kind].qx;k++)
{
x-=direction[dirt][0];
y-=direction[dirt][1];
}
for(k=0;(unsigned)k<strlen(qx1[kind].qx);k++)
{
x+=direction[dirt][0];
y+=direction[dirt][1];
if(!inside(x,y))return(false);
if(chessboard[x][y].step>0&&chessboard[x][y].color==color)c='2';
else if(chessboard[x][y].step>0)c='1';
else c='0';
if(c=='0'&&qx1[kind].qx[k]=='x')continue;
if(c!=qx1[kind].qx[k])return(false);
}
return true;
}
int value_qx1(NODE chessboard[][],int x,int y,int dirt, int color)
{
int i;
for(i=0;i<qx1_num;i++)
if(macth1(chessboard,x,y,dirt,i,color))
return qx1[i].value;
return 0;
}
bool macth2(NODE chessboard[][],int x,int y,int dirt, int kind,int color)
{
int k;
char c;
char *p;
p=strchr(qx2[kind].qx,'x');
for(k=0;k<=p-qx2[kind].qx;k++)
{
x-=direction[dirt][0];
y-=direction[dirt][1];
}
for(k=0;(unsigned)k<strlen(qx2[kind].qx);k++)
{
x+=direction[dirt][0];
y+=direction[dirt][1];
if(!inside(x,y))return false;
if(chessboard[x][y].step>0&&chessboard[x][y].color==color)c='2';
else if(chessboard[x][y].step>0)c='1';
else c='0';
if(c=='0'&&qx2[kind].qx[k]=='x')continue;
if(c!=qx2[kind].qx[k])return(false);
}
return true;
}
int value_qx2(NODE chessboard[][],int x,int y,int dirt, int color)
{
int i;
for(i=0;i<qx2_num;i++)
if(macth2(chessboard,x,y,dirt,i,color))
return qx2[i].value;
return 0;
}
void AI(NODE chessboard[][], int *x,int *y,int color)
{
int max=0;
int maxi,maxj;
int i,j,k;
int probability=1;
int value[][]={ 0};
int valueattack[][]={ { 0}};
int valuedefent[][]={ { 0}};
for(i=0;i<;i++)
for(j=0;j<;j++)
{
if(chessboard[i][j].step>0)continue;
for(k=0;k<8;k++)
valuedefent[i][j]+=value_qx1(chessboard,i,j,k,color);
if(maxi<valuedefent[i][j])
maxi=valuedefent[i][j];
}
for(i=0;i<;i++)
for(j=0;j<;j++)
{
if(chessboard[i][j].step>0)continue;
for(k=0;k<8;k++)
valueattack[i][j]+=value_qx2(chessboard,i,j,k,color);
if(maxj<valuedefent[i][j])
maxj=valuedefent[i][j];
}
if(rand()%(maxi+maxj+1)>maxi)
{
attacktrend=1;
defenttrend=1;
}
else
{
attacktrend=1;
defenttrend=2;
}
for(i=0;i<;i++)
for(j=0;j<;j++)
{
value[i][j]=valuedefent[i][j]*defenttrend+valueattack[i][j]*attacktrend;
if(max<value[i][j])
{
max=value[i][j];
maxi=i;
maxj=j;
probability=1;
}
else if(max==value[i][j])
{
if(rand()%(probability+1)<probability)
probability++;
else
{
probability=1;
max=value[i][j];
maxi=i;
maxj=j;
}
}
}
*x=maxi;
*y=maxj;
}
//-------------------------------------------------------------------------------------------------
bool vshuman;
void Vs(bool human)
{
int i,j;
int color=1;
int lastx,lasty;
int computer;
NODE chessboard[][]={ { 0,0}};
if(!human)
{
showsubmenu();
switch(getchoose(1,3))
{
case 1:
computer=0;
attacktrend=1;
defenttrend=1;
break;
case 2:
computer=1;
attacktrend=1;
defenttrend=2;
break;
case 3:return;
}
}
for(i=0;i<;i++)
for(j=0;j<;j++)
chessboard[i][j].step=0;
cursor.x=7;
cursor.y=7;
quit=false;
system("cls");
printf("\n");
printchessboard(chessboard);
for(i=1;i<=;)
{
gotoxy(0,0);
textcolor(TEXTS);
printf(" 第%d手, ",i);
if(color==1)printf("黑棋下");
else printf("白棋下");
regret=false;
if(i>1)
{
if(color!=computer||human)getmove(chessboard);
else
{
lastx=cursor.x;
lasty=cursor.y;
AI(chessboard,&cursor.x,&cursor.y,color);
renew(chessboard,lastx,lasty);
}
}
if(quit)return;
if(regret)
{
beback(chessboard,i);
if(i>2)i-=2;
else if(i==2)
{
i=1; color=(color+1)%2;
}
}
else
{
chessboard[cursor.x][cursor.y].step=i++;
chessboard[cursor.x][cursor.y].color=color;
renew(chessboard,cursor.x,cursor.y);
color=(color+1)%2;
}
if(win(chessboard,cursor.x,cursor.y,(color+1)%2)&&!regret)
{
textcolor(TEXTS);
gotoxy(0,0);
printf(" ");
gotoxy(0,0);
if(color==1)printf(" 白棋赢了!");
else printf(" 黑棋赢了!");
getch();
return;
}
}
gotoxy(0,0);
printf(" ");
gotoxy(0,0);
printf(" 平局!");
}
int main()
{
srand((unsigned)time(NULL));
for(;;)
{
showmenu();
switch(getchoose(1,3))
{
case 1:Vs(false);break;
case 2:Vs(true);break;
case 3:printf("\n");return 0;
}
}
return 0;
}
用C++编写的小游戏源代码
五子棋的代码:#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
using namespace std;
const int N=; //*的棋盘
const char ChessBoardflag = ' '; //棋盘标志
const char flag1='o'; //玩家1或电脑的棋子标志
const char flag2='X'; //玩家2的棋子标志
typedef struct Coordinate //坐标类
{
int x; //代表行
int y; //代表列
}Coordinate;
class GoBang //五子棋类
{
public:
GoBang() //初始化
{
InitChessBoard();
}
void Play() //下棋
{
Coordinate Pos1; // 玩家1或电脑
Coordinate Pos2; //玩家2
int n = 0;
while (1)
{
int mode = ChoiceMode();
while (1)
{
if (mode == 1) //电脑vs玩家
{
ComputerChess(Pos1,flag1); // 电脑下棋
if (GetVictory(Pos1, 0, flag1) == 1) //0表示电脑,真表示获胜
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
else //玩家1vs玩家2
{
PlayChess(Pos1, 1, flag1); // 玩家1下棋
if (GetVictory(Pos1, 1, flag1)) //1表示玩家1
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
}
cout << "***再来一局***" << endl;
cout << "y or n :";
char c = 'y';
cin >> c;
if (c == 'n')
break;
}
}
protected:
int ChoiceMode() //选择模式
{
int i = 0;
system("cls"); //系统调用,清屏
InitChessBoard(); //重新初始化棋盘
cout << "***0、源码源代退出 1、棋c棋富贵指标公式源码电脑vs玩家 2、源码源代源码安装 git玩家vs玩家***" << endl;
while (1)
{
cout << "请选择:";
cin >> i;
if (i == 0) //选择0退出
exit(1);
if (i == 1 || i == 2)
return i;
cout << "输入不合法" << endl;
}
}
void InitChessBoard() //初始化棋盘
{
for (int i = 0; i < N + 1; ++i)
{
for (int j = 0; j < N + 1; ++j)
{
_ChessBoard[i][j] = ChessBoardflag;
}
}
}
void PrintChessBoard() //打印棋盘,棋c棋这个函数可以自己调整
{
system("cls"); //系统调用,源码源代清空屏幕
for (int i = 0; i < N+1; ++i)
{
for (int j = 0; j < N+1; ++j)
{
if (i == 0) //打印列数字
{
if (j!=0)
printf("%d ",棋c棋 j);
else
printf(" ");
}
else if (j == 0) //打印行数字
printf("%2d ", i);
else
{
if (i < N+1)
{
printf("%c |",_ChessBoard[i][j]);
}
}
}
cout << endl;
cout << " ";
for (int m = 0; m < N; m++)
{
printf("--|");
}
cout << endl;
}
}
void PlayChess(Coordinate& pos, int player, int flag) //玩家下棋
{
PrintChessBoard(); //打印棋盘
while (1)
{
printf("玩家%d输入坐标:", player);
cin >> pos.x >> pos.y;
if (JudgeValue(pos) == 1) //坐标合法
break;
cout << "坐标不合法,重新输入" << endl;
}
_ChessBoard[pos.x][pos.y] = flag;
}
void ComputerChess(Coordinate& pos,源码源代 char flag) //电脑下棋
{
PrintChessBoard(); //打印棋盘
int x = 0;
int y = 0;
while (1)
{
x = (rand() % N) + 1; //产生1~N的随机数
srand((unsigned int) time(NULL));
y = (rand() % N) + 1; //产生1~N的随机数
srand((unsigned int) time(NULL));
if (_ChessBoard[x][y] == ChessBoardflag) //如果这个位置是空的,也就是棋c棋没有棋子
break;
}
pos.x = x;
pos.y = y;
_ChessBoard[pos.x][pos.y] = flag;
}
int JudgeValue(const Coordinate& pos) //判断输入坐标是不是合法
{
if (pos.x > 0 && pos.x <= N&&pos.y > 0 && pos.y <= N)
{
if (_ChessBoard[pos.x][pos.y] == ChessBoardflag)
{
return 1; //合法
}
}
return 0; //非法
}
int JudgeVictory(Coordinate pos, char flag) //判断有没有人胜负(底层判断)
{
int begin = 0;
int end = 0;
int begin1 = 0;
int end1 = 0;
//判断行是否满足条件
(pos.y - 4) > 0 ? begin = (pos.y - 4) : begin = 1;
(pos.y + 4) >N ? end = N : end = (pos.y + 4);
for (int i = pos.x, j = begin; j + 4 <= end; j++)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i][j + 1] == flag&&
_ChessBoard[i][j + 2] == flag&&_ChessBoard[i][j + 3] == flag&&
_ChessBoard[i][j + 4] == flag)
return 1;
}
//判断列是否满足条件
(pos.x - 4) > 0 ? begin = (pos.x - 4) : begin = 1;
(pos.x + 4) > N ? end = N : end = (pos.x + 4);
for (int j = pos.y, i = begin; i + 4 <= end; i++)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j] == flag&&
_ChessBoard[i + 2][j] == flag&&_ChessBoard[i + 3][j] == flag&&
_ChessBoard[i + 4][j] == flag)
return 1;
}
int len = 0;
//判断主对角线是否满足条件
pos.x > pos.y ? len = pos.y - 1 : len = pos.x - 1;
if (len > 4)
len = 4;
begin = pos.x - len; //横坐标的起始位置
begin1 = pos.y - len; //纵坐标的起始位置
pos.x > pos.y ? len = (N - pos.x) : len = (N - pos.y);
if (len>4)
len = 4;
end = pos.x + len; //横坐标的结束位置
end1 = pos.y + len; //纵坐标的结束位置
for (int i = begin, j = begin1; (i + 4 <= end) && (j + 4 <= end1); ++i, ++j)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j + 1] == flag&&
_ChessBoard[i + 2][j + 2] == flag&&_ChessBoard[i + 3][j + 3] == flag&&
_ChessBoard[i + 4][j + 4] == flag)
return 1;
}
//判断副对角线是否满足条件
(pos.x - 1) >(N - pos.y) ? len = (N - pos.y) : len = pos.x - 1;
if (len > 4)
len = 4;
begin = pos.x - len; //横坐标的起始位置
begin1 = pos.y + len; //纵坐标的起始位置
(N - pos.x) > (pos.y - 1) ? len = (pos.y - 1) : len = (N - pos.x);
if (len>4)
len = 4;
end = pos.x + len; //横坐标的结束位置
end1 = pos.y - len; //纵坐标的结束位置
for (int i = begin, j = begin1; (i + 4 <= end) && (j - 4 >= end1); ++i, --j)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j - 1] == flag&&
_ChessBoard[i + 2][j - 2] == flag&&_ChessBoard[i + 3][j - 3] == flag&&
_ChessBoard[i + 4][j - 4] == flag)
return 1;
}
for (int i = 1; i < N + 1; ++i) //棋盘有没有下满
{
for (int j =1; j < N + 1; ++j)
{
if (_ChessBoard[i][j] == ChessBoardflag)
return 0; //0表示棋盘没满
}
}
return -1; //和棋
}
bool GetVictory(Coordinate& pos, int player, int flag) //对JudgeVictory的一层封装,得到具体那个玩家获胜
{
int n = JudgeVictory(pos,源码源代 flag); //判断有没有人获胜
if (n != 0) //有人获胜,0表示没有人获胜
{
PrintChessBoard();
if (n == 1) //有玩家赢棋
{
if (player == 0) //0表示电脑获胜,棋c棋小说php源码1表示玩家1,源码源代2表示玩家2
printf("***电脑获胜***\n");
else
printf("***恭喜玩家%d获胜***\n", player);
}
else
printf("***双方和棋***\n");
return true; //已经有人获胜
}
return false; //没有人获胜
}
private:
char _ChessBoard[N+1][N+1];
};
扩展资料:
设计思路
1、进行问题分析与设计,棋c棋计划实现的功能为,开局选择人机或双人对战,idea jar 源码确定之后比赛开始。
2、比赛结束后初始化棋盘,询问是跑酷游戏 源码否继续比赛或退出,后续可加入复盘、悔棋等功能。
3、整个过程中,涉及到了棋子和棋盘两种对象,同时要加上人机对弈时的AI对象,即涉及到三个对象。
求助高手,求一个C语言五子棋的代码 带流程图的!
/* 程序中用到的库函数所在头文件应用 #include 命令包含进来 */
#include <stdio.h>
#include <bios.h>
#include <ctype.h>
#include <conio.h>
#include <dos.h>
/