c语言:设计一个五子棋游戏,无需人机对战,只要人人对战就可以。

c语言:设计一个五子棋游戏,无需人机对战,只要人人对战就可以。,第1张

把一个棋盘看着一个矩阵如25行25列用二维数组arry[25][25]表示,初始化数组所有数据为0;当用户1 在某个arry[i][j]位置输下子后,将arry[i][j]=1;赋值1,用户2下子的位置arry[i][j]=2;同时我们还可以设置一个标识数组,来识别某个位置是否下子了flag[25][25]={0};初始化0;

当用户1在arry[i][j]处下子后同时把falg[i][j]=1;来标识该处已经下子了所以下次就不能再在此处下子如要打印出棋谱只需吧arry[i][j]打印出来, 为1的地方就是w ,,,为2的地方就是b,,为0的地方表示该处未下子

我手头上有一个不能人机对战的用java编写的界面,可以判定输赢的。

//界面代码

import javaawt;

import javaxswing;

import javaawteventWindowAdapter;

import javaawteventWindowEvent;

import javaawtevent;

import javaxswing;

class drawChessBoard extends Canvas

{

//Image image;

Image image_black,image_white,image_point;

ImageIcon imageIcon_black = new ImageIcon("E:/workspace/五子棋/res/Image/blackjpg");

ImageIcon imageIcon_white = new ImageIcon("E:/workspace/五子棋/res/Image/whitejpg");

ImageIcon imageIcon_point = new ImageIcon("E:/workspace/五子棋/res/pointjpg");

isWin win = new isWin();

int locate =0;

int count =0;

static int drawCount = 0;

static String str;

public void paint(Graphics g)

{

int row = 0,column = 0;

chessboard chessBoard = new chessboard();

drawChessBoard draw = new drawChessBoard();

image_black = imageIcon_blackgetImage();

image_white = imageIcon_whitegetImage();

image_point = imageIcon_pointgetImage();

int temp_x = 0,temp_y = 0;

//画棋盘

for(row = 0;row < 16;row ++)

{

gdrawLine(0,40 + row 40, 640, 40 + row 40);

gdrawLine(40 + row 40, 0, 40 + row 40, 640);

// gdrawImage(image_point,317,317,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

// gdrawImage(image_point,477,157,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

// gdrawImage(image_point,157,157,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

// gdrawImage(image_point,157,477,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

// gdrawImage(image_point,477,477,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

for(column = 0;column < 16;column ++)

{

if( chessboardchess_state[row][column] == 1)

{

gdrawImage(image_black,20 + row 40,20 + column 40,imageIcon_blackgetIconWidth(),imageIcon_blackgetIconHeight(),imageIcon_blackgetImageObserver());

}

else if(chessboardchess_state[row][column] == 2)

{

gdrawImage(image_white,20 + row 40,20 + column 40,imageIcon_whitegetIconWidth(),imageIcon_whitegetIconHeight(),imageIcon_whitegetImageObserver());

}

}

}

//画棋子

if(count != 0 && chessboardisStart)

{

if(chessboardcoordinate_x / 40 >= 0 && chessboardcoordinate_x / 40 < 16 && chessboardcoordinate_y / 40 >= 0 && chessboardcoordinate_y / 40 < 16)

{

if(chessboardisBlack && chessboardchess_state[chessboardcoordinate_x / 40][chessboardcoordinate_y / 40] == 0 )

{

gdrawImage(image_black,20 + chessBoardcoordinate_x,20 + chessBoardcoordinate_y,imageIcon_blackgetIconWidth(),imageIcon_blackgetIconHeight(),imageIcon_blackgetImageObserver());

chessboardisBlack = false;

chessboardisWhite = true;

chessboardchess_state[ chessboardcoordinate_x / 40 ][ chessboardcoordinate_y / 40 ] = 1;

// chessboardchess_state[chessboardcoordinate_x / 40 - 1][chessboardcoordinate_y / 40 - 1] = 2;

//判断黑子是否赢

str = winwinJudge( chessboardcoordinate_x / 40,chessboardcoordinate_y / 40 );

}

if(chessboardisWhite && chessboardchess_state[chessboardcoordinate_x / 40][chessboardcoordinate_y / 40] == 0 )

{

gdrawImage(image_white,20 + chessBoardcoordinate_x,20 + chessBoardcoordinate_y,imageIcon_whitegetIconWidth(),imageIcon_whitegetIconHeight(),imageIcon_whitegetImageObserver());

// gdrawImage(image_white,temp_x - 40,temp_y - 40,imageIcon_whitegetIconWidth(),imageIcon_whitegetIconHeight(),imageIcon_whitegetImageObserver());

chessboardisWhite = false;

chessboardisBlack = true;

chessboardchess_state[chessboardcoordinate_x / 40][chessboardcoordinate_y / 40] = 2;

//判断白子是否赢

str = winwinJudge(chessboardcoordinate_x / 40,chessboardcoordinate_y / 40 );

}

}

}

count ++;

}

public void update(Graphics g)

{

chessboard chessBoard = new chessboard();

//覆盖问题

if((chessboardisChange || chessboardchess_state[chessboardcoordinate_x / 40][chessboardcoordinate_y / 40] == 0) && chessboardisStart )

{

superupdate(g);

chessboardisChange = false;//重写update方法

}

}

}

public class chessboard extends Frame implements MouseListener,ActionListener,MouseMotionListener

{

Button button = new Button("开始游戏");

Button button_exit = new Button("退出游戏");

static boolean isChange = false;

static boolean isStart = false;

drawChessBoard draw = new drawChessBoard();

int i ,j;

Panel panel = new Panel(),p;

static boolean isBlack = true,isWhite = false;

static boolean isPlayer = true,isComputer = false;

static int coordinate_x = 0,coordinate_y = 0;

static int chess_state[][] = new int[17][17];

JRadioButton black_radio,white_radio;

Label result_label;

chessboard(String name)

{

for(i = 0;i < 17;i ++)

{

for(j = 0;j < 17;j ++)

{

chess_state[i][j] = 0;

}

}

Color color = new Color(210,210,210);

paneladd(button);

panelsetLayout(null);

result_label = new Label();//创建标签

black_radio = new JRadioButton("黑棋",false);

white_radio = new JRadioButton("白棋",false);

black_radiosetBounds(680,80,40,20);

black_radiosetBounds(720,80,40,20);

result_labelsetBounds(680,220,80,40);

buttonsetBounds(680,100,80,40);

button_exitsetBounds(680,160,80,40);

thisadd(button);

thisadd(button_exit);

thisadd(result_label);

thisadd(black_radio);

thisadd(white_radio);

p = new Panel();

psetLayout(null);

psetBackground(color);

// thisadd(drawchess);

thisadd(draw);

//

thissetBounds(300, 20, 800, 700);

drawaddMouseListener(this);//监听draw

drawaddMouseMotionListener(this);

buttonaddActionListener(this);

button_exitaddActionListener(this);

thisaddWindowListener(new MyWindowListener());

thissetVisible(true);

}

chessboard()

{

}

public void actionPerformed(ActionEvent e)

{

if((Button)egetSource() == button_exit)

{

Systemexit(0);

}

else if ((Button)egetSource() == button)

{

isStart = true;

result_labelsetText("游戏进行中");

for(i = 0;i < 17;i ++)

{

for(j = 0;j < 17;j ++)

{

chessboardchess_state[i][j] = 0;

}

}

drawrepaint();

}

}

public void mouseClicked(MouseEvent e)

{

}

public void mouseEntered(MouseEvent e)

{

}

public void mousePressed(MouseEvent e)

{

chessboard chess = new chessboard();

if(chesscoordinate_x <= 640 && chesscoordinate_y <= 640 && chesscoordinate_x >= 0 && chesscoordinate_y >= 0 )

{

drawrepaint();

}

}

public void mouseReleased(MouseEvent e)

{

if(isStart == false)

{

result_labelsetText(drawChessBoardstr);

}

//电脑落子调用mouseReleased

/

if(chessboardisComputer)

{

drawrepaint();

}

/

}

public void mouseExited(MouseEvent e)

{

}

public void mouseDragged(MouseEvent e)

{

// Systemexit(0);

}

public void mouseMoved(MouseEvent e){

Integer integ1,integ2 ;

chessboardisChange = true;

coordinate_x = egetX();

coordinate_y = egetY();

// integ1 = new Integer(coordinate_x / 40);

// integ2 = new Integer(coordinate_y / 40);

// result_labelsetText(integ1toString()+" "+integ2toString());

if(coordinate_x <= 20)

{

coordinate_x = (coordinate_x / 40 + 1) 40;

}

if(coordinate_y <= 20)

{

coordinate_y = (coordinate_y / 40 + 1) 40;

}

if( coordinate_x % 40 <=20 )

{

coordinate_x = (coordinate_x / 40 -1) 40;

}

else

{

coordinate_x = (coordinate_x / 40 ) 40;

}

if( coordinate_y % 40 <=20 )

{

coordinate_y = (coordinate_y / 40 - 1 ) 40;

}

else

{

coordinate_y = (coordinate_y / 40 ) 40;

}

}

public static void main(String args[])

{

chessboard cb = new chessboard("五子棋");

}

}

class MyWindowListener extends WindowAdapter

{

public void windowClosing(WindowEvent e)

{

Systemexit(0);

}

}

//判断输赢

public class isWin {

//static int count = 0;

public String winJudge(int row,int col)

{

chessboard chessBoard = new chessboard();

int index,count = 1;

int n = 0 ,flag = 1;

boolean isOneside = true, isAnotherside = true;

if(chessboardchess_state[row][col] != 0)

{

//水平方向判断

for(index = 0;index < 5;index ++)

{

//向落子点右边扫描

while(isOneside)

{

if(n == 0) count = 1;

if(row + count <= 16 && chessboardchess_state[ row + count][ col ] == chessboardchess_state[ row ][ col ] )

{

flag ++;

n ++;

count ++;

}

else

{

isOneside = false;

n = 0;

break;

}

}

//向落子点左边扫面

while(!isOneside)

{

if(n == 0) count = 1;

if(row - count >= 0 && chessboardchess_state[ row - count ][ col ] == chessboardchess_state[ row ][ col ] && !isOneside && row > 1)

{

flag ++;

count ++;

n ++;

}

else

{

isOneside = true;

n = 0;

break;

}

}

//如果左边没有落子或者落了颜色不同的子

if(flag >= 5)

{

chessboardisStart = false;

if(chessboardchess_state[row][col] == 1)

{

return "黑子赢了";

}

else

{

return "白子赢了";

}

}

if(flag < 5)

flag = 1;

}

//竖直方向判断

for(index = 0;index < 5;index ++)

{

while(isOneside)

{

if( n == 0 )

{

count = 1;

}

if(col + count <= 16 && chessboardchess_state[ row ][ col + count ] == chessboardchess_state[ row ][ col ] )

{

flag ++;

n ++;

count ++;

}

else

{

isOneside = false;

n = 0;

break;

}

}

//向落子点左边扫面

while(!isOneside)

{

if(n == 0) count = 1;

if(col - count >= 0 && chessboardchess_state[ row ][ col - count ] == chessboardchess_state[ row ][ col ] && !isOneside )

{

flag ++;

n ++;

count ++;

}

else

{

isOneside = true;

n = 0;

break;

}

}

//如果左边没有落子或者落了颜色不同的子

if(flag >= 5)

{

chessboardisStart = false;

if(chessboardchess_state[row][col] == 1)

{

return "黑子赢了";

}

else if(chessboardchess_state[row][col] == 2)

{

return "白子赢了";

}

}

if(flag < 5)

flag = 1;

}

//斜向上方向判断

for(index = 0;index < 5;index ++)

{

//向落子点右边扫描

while(isOneside)

{

if(n == 0) count = 1;

if( chessboardchess_state[ row + count][ col + count ] == chessboardchess_state[ row ][ col ] )

{

flag ++;

n ++;

count ++;

}

else

{

isOneside = false;

n = 0;

break;

}

}

//向落子点左边扫面

while(!isOneside)

{

if(n == 0) count = 1;

if(row - count >= 0 && col - count >= 0 && chessboardchess_state[ row - count][ col - count ] == chessboardchess_state[ row ][ col ] && !isOneside)

{

flag ++;

count ++;

n ++;

}

else

{

isOneside = true;

n = 0;

break;

}

}

//如果左边没有落子或者落了颜色不同的子

if(flag >= 5)

{

chessboardisStart = false;

if(chessboardchess_state[row][col] == 1)

{

return "黑子赢了";

}

else

{

return "白子赢了";

}

}

if(flag < 5)

flag = 1;

}

//斜向下方向判断

for(index = 0;index < 5;index ++)

{

//向落子点右边扫描

while(isOneside)

{

if(n == 0) count = 1;

if(row - count >= 0 && col + count <= 16 && chessboardchess_state[ row - count][ col + count] == chessboardchess_state[ row ][ col ] )

{

flag ++;

n ++;

count ++;

}

else

{

isOneside = false;

n = 0;

break;

}

}

//向落子点左边扫面

while(!isOneside)

{

if(n == 0) count = 1;

if(row + count <= 16 && col - count >= 0 && chessboardchess_state[ row + count][ col - count ] == chessboardchess_state[ row ][ col ] && !isOneside)

{

flag ++;

count ++;

n ++;

}

else

{

isOneside = true;

n = 0;

break;

}

}

//如果左边没有落子或者落了颜色不同的子

if(flag >= 5)

{

chessboardisStart = false;

if(chessboardchess_state[row][col] == 1)

{

return "黑子赢了";

}

else

{

return "白子赢了";

}

}

if(flag < 5)

flag = 1;

}

}//if

return "0";

}

public static void main(String args[])

{

isWin winjudge = new isWin();

//winjudgewinJudge(row, col);

}

}

几个问题:

weizhi这个存放坐标的数组需要用二维数组,否则只能存放一行位置;

初始化棋盘的动作从draw函数里移出来,放在main函数里做,draw只负责把当前棋盘和棋子画出来,初始化棋盘就是输入棋盘的大小;

存放棋子的二维数组p需要初始化;

draw画棋盘的时候,要判断p数组对应存放了什么字符来决定输出什么内容;

判断棋子超过棋盘范围,只需要检查输入的坐标x和y是否都比棋盘小就可以了,放在for循环里,每次都会被执行到。

代码改了一下,你看一看。

#include<stdioh>

#define N 20

#define X 10

struct zuobiao

{

        int x;

        int y;

};

struct zuobiao weizhi[N][N];

char p[N][N];

int len;

void draw ();

void main()

{

        int i,j;

        int a,b;

        memset(p, 0, NN);

        printf ("Please input the five_chess's board\n");

        scanf ("%d", &len);

        draw();

        printf ("请输入一个坐标:例如 3,4 \n");

        scanf("%d,%d", &a,&b);

        if(a >= len || b >= len)

            printf("棋子超过棋盘范围\n");

        for(i=0;i<len;i++)

        {

                for(j=0;j<len;j++)

                {

                        if ((weizhi[i][j]x==a) && (weizhi[i][j]y==b))

                        {

                                p[a][b]='@';

                                draw();

                        }

                }

        }

}

void draw()

{

        int i,j,k;

        for (i = 0; i<len; i++)

        {

            if(i==0)

                printf("%3d",i);

            else

                printf("%2d", i );

        }

        printf("\n");

        for (i = 0; i<len; i++)

        {

            printf ("%2d", i );

            for (k = 0; k< len; k++)

            {

                if(p[i][k] == 0)

                    printf ("十");

                else

                    printf ("%c ", p[i][k]);

                weizhi[i][k]x=i;

                weizhi[i][k]y=k;

            }

            printf ("\n");

        }

}

以上就是关于c语言:设计一个五子棋游戏,无需人机对战,只要人人对战就可以。全部的内容,包括:c语言:设计一个五子棋游戏,无需人机对战,只要人人对战就可以。、如何用C语言做一个五子棋程序要有源代码。、C语言—五子棋求大神帮忙看看,设计是按输入的坐标打印棋子。可打不出来等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9317342.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存