五子棋c语言最简单代码(c语言五子棋easyx)
c语言五子棋代码,
package day17.gobang;
import java.util.Arrays;
public class GoBangGame {
public static final char BLANK='*';
public static final char BLACK='@';
public static final char WHITE='O';
public static final int MAX = 16;
private static final int COUNT = 5;
//棋盘
private char[][] board;
public GoBangGame() {
}
//开始游戏
public void start() {
board = new char[MAX][MAX];
//把二维数组都填充‘*’
for(char[] ary: board){
Arrays.fill(ary, BLANK);
}
}
public char[][] getChessBoard(){
return board;
}
public void addBlack(int x, int y) throws ChessExistException{
//@
//char blank = '*';
//System.out.println( x +"," + y + ":" + board[y][x] + "," + BLANK);
if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子
board[y][x] = BLACK;
return;
}
throw new ChessExistException("已经有棋子了!");
}
public void addWhite(int x, int y)
throws ChessExistException{
if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子
board[y][x] = WHITE;
return;
}
throw new ChessExistException("已经有棋子了!");
}
//chess 棋子:'@'/'O'
public boolean winOnY(char chess, int x, int y){
//先找到y方向第一个不是 blank的棋子
int top = y;
while(true){
if(y==0 || board[y-1][x]!=chess){
//如果y已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break;
}
y--;
top = y;
}
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0;
y = top;
while(true){
if(y==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break;
}
count++;
y++;
}
return count==COUNT;
}
//chess 棋子:'@'/'O'
public boolean winOnX(char chess, int x, int y){
//先找到x方向第一个不是 blank的棋子
int top = x;
while(true){
if(x==0 || board[y][x-1]!=chess){
//如果x已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break;
}
x--;
top = x;
}
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0;
x = top;
while(true){
if(x==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break;
}
count++;
x++;
}
return count==COUNT;
}
//chess 棋子:'@'/'O'
public boolean winOnXY(char chess, int x, int y){
//先找MAX向第一个不是 blank的棋子
int top = y;
int left = x;
while(true){
if(x==0 || y==0 || board[y-1][x-1]!=chess){
//如果x已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break;
}
x--;
y--;
top = y;
left=x;
}
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0;
x = left;
y = top;
while(true){
if(x==MAX || y==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break;
}
count++;
x++;
y++;
}
return count==COUNT;
}
//chess 棋子:'@'/'O'
public boolean winOnYX(char chess, int x, int y){
//先找到x方向第一个不是 blank的棋子
int top = y;
int left = x;
while(true){
if(x==MAX-1 || y==0 || board[y-1][x+1]!=chess){
//如果x已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break;
}
x++;
y--;
top = y;
left=x;
}
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0;
x = left;
y = top;
while(true){
if(x==0 || y==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break;
}
count++;
x--;
y++;
}
return count==COUNT;
}
public boolean whiteIsWin(int x, int y) {
//在任何一个方向上赢了,都算赢
return winOnY(WHITE, x, y) ||
winOnX(WHITE, x, y) ||
winOnXY(WHITE, x, y) ||
winOnYX(WHITE, x, y);
}
public boolean blackIsWin(int x, int y) {
return winOnY(BLACK, x, y) ||
winOnX(BLACK, x, y) ||
winOnXY(BLACK, x, y) ||
winOnYX(BLACK, x, y);
}
}
人人对弈的简单五子棋程序 c语言 棋盘是点 语言200行左右
五子棋程序:
#includeiostream
using namespace std;
int Hsheng(char a[][15]);//判断o子是否获胜的函数
int Bsheng(char a[][15]);//判断x子是否获胜的函数
int he(char a[][15]);//判断是否平局(也就是棋盘下满了)的函数
void qipan(char a[15][15])//执行输出棋盘命令
{ for(int i=0;i15;i++) //打印棋盘
{ for(int j=0;j15;j++)
couta[i][j];
coutendl; } }
int main()
{ char a[15][15]; int x,y;
for(int i=0;i15;i++)
for(int j=0;j15;j++)
a[i][j]=' '; qipan(a);
while(1)//用循环语句执行o,x交替下子,这些while语句看起来似乎是个死循环~实际上都会经过break结束
{ int a1=1;
while(1)
{ for(;a1;)
{ cout"请输入o子下的位置:"; //输入o子的位置
cinxy; if(a[x][y]=='o'||a[x][y]=='x')//判断是否已有子
{cout"已有子请重下"",";continue;}
else if(x=15||y=15){cout"输入错误请重输"",";continue;}
else { a[x][y]='o'; a1=0;}
} break;}
qipan(a);//下好o子后将棋盘显示
if(Hsheng(a))//判断o子是否已经获胜
{ cout"o子获胜"endl; break; }
while(1)//下x子
{ cout"请输入x子下的位置:";
cinxy;
if(a[x][y]=='o'||a[x][y]=='x'||x=15||y=15)
{ for( ; a[x][y]=='o'||a[x][y]=='x'; )
{ cout"已有子请重下";
cout"请输入x子下的位置:";
cinxy;continue; }
for ( ; x=15||y=15||x; )
{ cout"输入错误请重输"","; //判断输入棋子位置是否正确
cout"请输入x子下的位置:";
cinxy;continue ;}
a[x][y]='x';break; }
else
{ a[x][y]='x'; break; } }
qipan(a);//再一次输出棋盘
if(Bsheng(a))//判断x子是否已经获胜
{ cout"x子获胜"endl; break; }
if(he(a))//判断是否平局
{ cout"平局"endl; break; } }
return 0; }
int Hsheng(char a[][15])
{ int i,j;//判断横着的5个是否都相等
for(i=0;i15;i++)
for(j=0;j15;j++)
if(a[i][j]=='o'a[i][j+1]=='o'a[i][j+2]=='o'a[i][j+3]=='o'a[i][j+4]=='o')
return 1;
for(j=0;j15;j++)//判断竖着的5个是否都相等
for(i=0;i15;i++)
if(a[i][j]=='o'a[i+1][j]=='o'a[i+2][j]=='o'a[i+3][j]=='o'a[i+4][j]=='o')
return 1;
for(i=0;i15;i++)//判断左斜5个
for(j=0;j15;j++)
if(a[i][j]=='o'a[i+1][j+1]=='o'a[i+2][j+2]=='o'a[i+3][j+3]=='o'a[i+4][j+4]=='o')
return 1;
for(i=0;i15;i++)//右斜5个
for(j=14;j3;j--)
if(a[i][j]=='H'a[i+1][j-1]=='o'a[i+2][j-2]=='o'a[i+3][j-3]=='o'a[i+4][j-4]=='o')
return 1;
return 0; }
int Bsheng(char a[][15])//同o,只是改字符
{ int i,j;
for(i=0;i15;i++)
for(j=0;j15;j++)
if(a[i][j]=='x'a[i][j+1]=='x'a[i][j+2]=='x'a[i][j+3]=='x'a[i][j+4]=='x')
return 1;
for(j=0;j15;j++)
for(i=0;i15;i++)
if(a[i][j]=='x'a[i+1][j]=='x'a[i+2][j]=='x'a[i+3][j]=='x'a[i+4][j]=='x')
return 1;
for(i=0;i15;i++)
for(j=0;j15;j++)
if(a[i][j]=='x'a[i+1][j+1]=='x'a[i+2][j+2]=='x'a[i+3][j+3]=='x'a[i+4][j+4]=='x')
return 1;
for(i=0;i15;i++)
for(j=14;j3;j--)
if(a[i][j]=='x'a[i+1][j-1]=='x'a[i+2][j-2]=='x'a[i+3][j-3]=='x'a[i+4][j-4]=='x')
return 1;
return 0; }
int he(char a[][15])
{ for(int i=0;i15;i++)
for(int j=0;j15;j++)
{ if(a[i][j]==' ')//当棋盘全部子都不是' '时才能return 1,即棋盘已下满
return 0;
}
return 1;
}
希望能帮到你!!
怎样用C语言编一个简单的五子棋游戏?
#include graphics.h
#include stdio.h
#include MATH.H
IMAGE* IMG;
IMAGE* IMG2;
IMAGE* IMG3;
IMAGE* whole;
bool mark = false;
int x = 0, y = 0;
int flag[15][15];
void show()
{
outtextxy(550, 100, "白方:");
outtextxy(550, 150, " 箭头移动");
outtextxy(550, 200, " 回车键落子");
outtextxy(550, 250, "黑方:");
outtextxy(550, 300, " ADWS移动");
outtextxy(550, 350, " 空格键落子");
}
int success1(int dir1, int dir2)
{
int number = 0;
int temp_x = x, temp_y = y;
while (((temp_x / 35 + dir1) = 0 (temp_x / 35 + dir1) 15) ((temp_y / 35 + dir2) = 0 (temp_y / 35 + dir2) 15) (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 1))
{
temp_x = temp_x + dir1 * 35;
temp_y = temp_y + dir2 * 35;
++number;
}
return number;
}
int success2(int dir1, int dir2)
{
int number = 0;
int temp_x = x, temp_y = y;
while (((temp_x / 35 + dir1) = 0 (temp_x / 35 + dir1) 15) ((temp_y / 35 + dir2) = 0 (temp_y / 35 + dir2) 15) (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 2))
{
temp_x = temp_x + dir1 * 35;
temp_y = temp_y + dir2 * 35;
++number;
}
return number;
}
int success1()
{
int number = 0;
number = success1(0, -1) + success1(0, 1);//上下
if (number 4)
{
number = success1(-1, 0) + success1(1, 0);//左右
if (number 4)
{
number = success1(-1, -1) + success1(1, 1);//左上右下
if (number 4)
{
number = success1(-1, 1) + success1(1, -1);//左下右上
}
}
}
return number;
}
int success2()
{
int number = 0;
number = success2(0, -1) + success2(0, 1);//上下
if (number 4)
{
number = success2(-1, 0) + success2(1, 0);//左右
if (number 4)
{
number = success2(-1, -1) + success2(1, 1);//左上右下
if (number 4)
{
number = success2(-1, 1) + success2(1, -1);//左下右上
}
}
}
return number;
}
void control()
{
char key = 0;
while (key != 27)
{
Sleep(10);
if (kbhit())
{
key = getch();
switch (key)
{
case VK_LEFT:
if (mark)
break;
if (x 0)
x = x - 35;
break;
case 'a':
case 'A':
if (!mark)
break;
if (x 0)
x = x - 35;
break;
case VK_RIGHT:
if (mark)
break;
if (x 490)
x = x + 35;
break;
case 'd':
case 'D':
if (!mark)
break;
if (x 490)
x = x + 35;
break;
case VK_UP:
if (mark)
break;
if (y 0)
y = y - 35;
break;
case 'w':
case 'W':
if (!mark)
break;
if (y 0)
y = y - 35;
break;
case VK_DOWN:
if (mark)
break;
if (y 490)
y = y + 35;
break;
case 's':
case 'S':
if (!mark)
break;
if (y 490)
y = y + 35;
break;
case VK_RETURN:
if (mark)
break;
if (flag[x / 35][y / 35] == 0)
{
putimage(whole, x + 6, y + 6, 31, 32, IMG2, 0, 0);
flag[x / 35][y / 35] = 1;
if (success1() = 4)
{
outtextxy(600, 50, "黑方 胜!");
key = 27;
}
mark = true;
}
break;
case VK_SPACE:
if (!mark)
break;
if (flag[x / 35][y / 35] == 0)
{
putimage(whole, x + 6, y + 6, 31, 31, IMG3, 0, 0);
flag[x / 35][y / 35] = 2;
if (success2() = 4)
{
outtextxy(600, 50, "白方 胜!");
key = 27;
}
mark = false;
}
break;
default:
break;
}
putimage(0, 0, whole);
putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);
}
}
}
void main()
{
setinitmode(0);
initgraph(800, 538);
SetWindowText(GetHWnd(), "五子棋20110327");
setcolor(0xffffff);
setfont(36, 0, "楷体_GB2312");
IMAGE* IMG1 = new IMAGE;
getimage(IMG1, "JPG", MAKEINTRESOURCE(102));//棋盘
putimage(0, 0, IMG1);
IMG2 = new IMAGE;
getimage(IMG2, "JPG", MAKEINTRESOURCE(103));//黑棋
IMG3 = new IMAGE;
getimage(IMG3, "JPG", MAKEINTRESOURCE(104));//白棋
IMG = new IMAGE;
getimage(IMG, "GIF", MAKEINTRESOURCE(101));//手形
whole = new IMAGE;
getimage(whole, 0, 0, 537, 537);
putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);
show();
control();
delete IMG1;
delete IMG2;
delete IMG3;
delete whole;
getch();
getch();
closegraph();
}