How this game is playe can be found at here.

 public class Connect4 {
char[][] board = new char[][]; public Connect4(char[][] board) {
this.board = board;
} public static void main(String[] args) {
Connect4 game = new Connect4(new char[][]);
game.fillBoard(' ');
Scanner input = new Scanner(System.in);
char player = 'X';
while (true) {
game.showGameBoard();
System.out.print("Player " + player + ", please enter the column where you'd like to drop your piece: ");
int col = input.nextInt();
if (game.tryDropPiece(col, player)) {
if (game.checkForWin()) {
System.out.println("Player " + player + " wins!");
game.showGameBoard();
return;
}
player = game.switchPlayer(player); // Switch players
}
}
} public char[][] fillBoard(char myChar) {
for (int row = ; row < board.length; row++) {
Arrays.fill(board[row], , board[row].length, myChar);
}
return board;
} public void showGameBoard() {
System.out.println();
for (int row = ; row < board.length; row++) {
System.out.print("|");
for (int col = ; col < board[row].length; col++) {
System.out.print(" " + board[row][col] + " |");
}
System.out.println();
}
} public boolean tryDropPiece(int col, char player) {
if (board[][col] != ' ') {
System.out.println("That column is already full.");
return false;
}
for (int row = board.length - ; row >= ; row--) {
if (board[row][col] == ' ') {
board[row][col] = player;
return true;
}
}
return false;
} public boolean checkForWin() {
boolean result = false;
// Check for win horizontally
for (int row = ; row < board.length; row++) {
for (int col = ; col < board[row].length - ; col++) {
if (board[row][col] != ' ' && board[row][col] == board[row][col + ]
&& board[row][col] == board[row][col + ] && board[row][col] == board[row][col + ]) {
return true;
}
}
}
// Check for win vertically
for (int col = ; col < board[].length; col++) {
for (int row = ; row < board.length - ; row++) {
if (board[row][col] != ' ' && board[row][col] == board[row + ][col]
&& board[row][col] == board[row + ][col] && board[row][col] == board[row + ][col]) {
return true;
}
}
} // Check for win diagonally, from top left
for (int row = ; row < board.length - ; row++) {
for (int col = ; col < board[row].length - ; col++) {
if (board[row][col] != ' ' && board[row][col] == board[row + ][col + ]
&& board[row][col] == board[row + ][col + ] && board[row][col] == board[row + ][col + ]) {
return true;
}
}
} // Check for win diagonally, from top right
for (int row = ; row < board.length - ; row++) {
for (int col = ; col < board[row].length; col++) {
if (board[row][col] != ' ' && board[row][col] == board[row + ][col - ]
&& board[row][col] == board[row + ][col - ] && board[row][col] == board[row + ][col - ]) {
return true;
}
}
}
return false; } public char switchPlayer(char currentPlayer) {
if (currentPlayer == 'X') {
return 'O';
} else {
return 'X';
}
}
}

Connect4 Game的更多相关文章

  1. ESP8266使用详解--基于Lua脚本语言

    这些天,,,,今天终于看到了希望,,,天道酬勤 先说实现的功能...让ESP8266连接无线网,然后让它建立服务器,,我的客户端连接上以后,发给客户端发数据模块打印到串口,,往ESP8266串口里发数 ...

  2. luasocket 接收数据

    在游戏客户端使用luasocket作为网络通信的手段, 有一点很蛋疼, 就是它的receive是阻塞的,  那界面就卡死在那里了,  不过有一个函数:settimeout(), 传入参数0, 表示如果 ...

  3. [知乎]这可能是最全面的龙芯3A3000处理器评测

    这可能是最全面的龙芯3A3000处理器评测 第一千零一个人   已关注 蓬岸 Dr.Quest . https://zhuanlan.zhihu.com/p/50716952 这里面链接很全. 立党 ...

  4. 【转帖】龙芯3A3000处理器深度评测:和Intel、AMD差距巨大

    龙芯3A3000处理器深度评测:和Intel.AMD差距巨大 https://www.eefocus.com/mcu-dsp/424623/r0 作者非计算机科班毕业 让我汗颜. 我计算机毕业都不知道 ...

  5. 国产龙芯3A3000处理器评测:与英特尔差距明显

    国产龙芯3A3000处理器评测:与英特尔差距明显 国产龙芯3A3000处理器评测:与英特尔差距明显 新浪财经APP缩小字体放大字体收藏微博微信分享579 新酷产品第一时间免费试玩,还有众多优质达人分享 ...

随机推荐

  1. 爬虫----异步---高性能爬虫----aiohttp 和asycio 的使用

    前情提要: 首先膜拜loco大佬 肯定有人像我一样.不会异步,发一下. 一:性能比对 多进程,多线程,(这里不建议使用,太消耗性能) 进程池和线程池 (可以适当的使用) 单线程+异步协程   (推荐使 ...

  2. Bzoj 2134: [国家集训队2011]单选错位(期望)

    2134: 单选错位 Time Limit: 10 Sec Memory Limit: 259 MB Description Input n很大,为了避免读入耗时太多,输入文件只有5个整数参数n, A ...

  3. 【luogu1016】旅行家的预算--模拟

    题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1D1D1.汽车油箱的容量CCC(以升为单位).每升汽油能行驶的距离D2D2D2.出发 ...

  4. Java面向对象3(K~O)

    K     正方形(SDUT 2444) import java.lang.reflect.Array; import java.util.*; public class Main { public ...

  5. devstack 使用openstack命令报错 The request you have made requires authentication. (HTTP 401) Missing value auth-url required for auth plugin password

    关联错误: The request you have made requires authentication. (HTTP 401) (Request-ID: req-88ad2cba-0f2d-4 ...

  6. SpringMVC框架下Web项目的搭建与部署

    这篇文章已被废弃. 现在,Deolin使用Maven构建项目,而不是下载Jar文件,使用Jetty插件调试项目,而不是外部启动Tomcat. SpringMVC比起Servlet/JSP方便了太多 W ...

  7. springboot的注解

    1.@ConfigurationProperties 功能:装载配置文件信息到实体 原理:aop,通知类型:?(方法或对象创建完后) 注意:作用于方法上可以不需要改源码(例如durid配置) 转载:h ...

  8. golang中文件以及文件夹路径相关操作

    获取目录中所有文件使用包: io/ioutil 使用方法: ioutil.ReadDir 读取目录 dirmane 中的所有目录和文件(不包括子目录) 返回读取到的文件的信息列表和读取过程中遇到的任何 ...

  9. mongodb的安装及使用

    1.MongoDB安装 安装包下载地址: https://www.mongodb.com/download-center/community 启动数据库:进入到mongd所在的bin目录,执行mong ...

  10. MySQL的那些坑

    1.  表名一定要区分大小写,不一致就会报错 2. 无隐式的类型转换 (比如对某数值进行排序时,原表字段存储却是varchar型,就会对该数值按字符串排序而非数值大小!) 3. group by 也能 ...