JAVA编程训练——Game: connect four
来源:互联网 发布:东盟共同体 知乎 编辑:程序博客网 时间:2024/06/03 14:28
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 0
- JAVA编程训练——Game: connect four
- JAVA编程训练——Game: playing a TicTacToe game
- JAVA编程训练——Game:scissor, rock, paper
- JAVA三星题之connect four
- The four number game
- JavaScript —— four
- JAVA编程训练——Current date and time
- connect four / tic-tak-toe
- Java编程技能训练
- Java编程技能训练
- 网络编程Winsock——connect
- [ZOJ 3047] Connect Four [模拟+博弈]
- 编程能力训练 —— 系列介绍
- 《Windows程序设计》笔记 —— Chapter Four
- 【LetCode——342】Power of four
- (编程训练)再回首,数据结构——顺序表上的编程训练
- (编程训练)再回首,数据结构——链表上的编程训练_实现多项式运算
- UNIX网络编程——非阻塞connect
- Vbs脚本
- ubuntu下不加index.php无法正常访问
- ffmpeg AV转换常用命令
- ThinkPHP 学习每日总结
- bootstrap 样式查阅参考文档
- JAVA编程训练——Game: connect four
- android 多线程Thread,Runnable,Handler,AsyncTask等之间的关系
- 《Java虚拟机原理图解》 1.1、class文件基本组织结构
- Python深入:stevedore简介
- 注解的知识总结
- 快速幂
- 委托使用心得
- Mac下同时安装多个版本的JDK
- xcode EditText 软键盘自动退出