java connect four,JAVA编程训练——Game: connect four

Connect four is a two-player board game in which the players alternately drop colored disks into a seven-column, six-row vertically-suspended grid.

The objective of the game is to connect four same-colored disks in a row, a column, or a diagonal before your opponent can do likewise. The program prompts two players to drop a RED or YELLOW disk alternately. Whenever a disk is dropped, the program redisplays the board on the console and determines the status of the game (win, draw, or continue).

import java.util.Date;

import java.util.Scanner;

import java.util.Calendar;

import javax.swing.JFrame;

public class TicIacIoe

{

char[][] block={{' ',' ',' ',' ',' ',' ',' '},{' ',' ',' ',' ',' ',' ',' '},{' ',' ',' ',' ',' ',' ',' '},{' ',' ',' ',' ',' ',' ',' '},{' ',' ',' ',' ',' ',' ',' '},{' ',' ',' ',' ',' ',' ',' '}};

TicIacIoe(){}

public void show()

{

for (int i = 0; i < 6; ++i)

{

for (int j = 0; j < 7; ++j)

System.out.print("|" + block[i][j]);

System.out.println("|");

}

System.out.println("---------------");

}

public void playr()

{

int i=0,j=0;

Scanner input = new Scanner(System.in);

while(true)

{

System.out.print("Drop a red disk at column (0-6):");

i=input.nextInt();

if(i>6)

System.out.println("Your input is wrong!");

else if(block[0][i]!=' ')

System.out.println("This column is full");

else

break;

}

for(j=5;j>=0;--j)

{

if(block[j][i]==' ')

{

block[j][i]='R';

break;

}

}

}

public void playy()

{

int i=0,j=0;

Scanner input = new Scanner(System.in);

while(true)

{

System.out.print("Drop a yellow disk at column (0-6):");

i=input.nextInt();

if(i>6)

System.out.println("Your input is wrong!");

else if(block[0][i]!=' ')

System.out.println("This column is full");

else

break;

}

for(j=5;j>=0;--j)

{

if(block[j][i]==' ')

{

block[j][i]='Y';

break;

}

}

}

public boolean check(char n)

{

for(int i=0;i<5;++i)

for(int j=0;j<6;++j)

if(block[i][j]==n&&block[i+1][j]==n&&block[i][j+1]==n&&block[i+1][j+1]==n)

return true;

return false;

}

public static void main(String[] args)

{

TicIacIoe ticIacIoe =new TicIacIoe();

int cnt=0;

while(true)

{

ticIacIoe.show();

ticIacIoe.playr();

ticIacIoe.show();

if(ticIacIoe.check('R'))

{

System.out.println("The red palyer won");

break;

}

cnt++;

ticIacIoe.playy();

ticIacIoe.show();

if(ticIacIoe.check('Y'))

{

System.out.println("The yellow palyer won");

break;

}

cnt++;

if(cnt==42)

{

System.out.println("It is a draw");

break;

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值