Connect4 Game
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的更多相关文章
- ESP8266使用详解--基于Lua脚本语言
这些天,,,,今天终于看到了希望,,,天道酬勤 先说实现的功能...让ESP8266连接无线网,然后让它建立服务器,,我的客户端连接上以后,发给客户端发数据模块打印到串口,,往ESP8266串口里发数 ...
- luasocket 接收数据
在游戏客户端使用luasocket作为网络通信的手段, 有一点很蛋疼, 就是它的receive是阻塞的, 那界面就卡死在那里了, 不过有一个函数:settimeout(), 传入参数0, 表示如果 ...
- [知乎]这可能是最全面的龙芯3A3000处理器评测
这可能是最全面的龙芯3A3000处理器评测 第一千零一个人 已关注 蓬岸 Dr.Quest . https://zhuanlan.zhihu.com/p/50716952 这里面链接很全. 立党 ...
- 【转帖】龙芯3A3000处理器深度评测:和Intel、AMD差距巨大
龙芯3A3000处理器深度评测:和Intel.AMD差距巨大 https://www.eefocus.com/mcu-dsp/424623/r0 作者非计算机科班毕业 让我汗颜. 我计算机毕业都不知道 ...
- 国产龙芯3A3000处理器评测:与英特尔差距明显
国产龙芯3A3000处理器评测:与英特尔差距明显 国产龙芯3A3000处理器评测:与英特尔差距明显 新浪财经APP缩小字体放大字体收藏微博微信分享579 新酷产品第一时间免费试玩,还有众多优质达人分享 ...
随机推荐
- Vue中用props给data赋初始值遇到的问题解决
Vue中用props给data赋初始值遇到的问题解决 更新时间:2018年11月27日 10:09:14 作者:yuyongyu 我要评论 这篇文章主要介绍了Vue中用props给dat ...
- Noip2003 提高组 神经网络
神经网络 题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神经网络的研究 ...
- java集合类-集合框架体系
集合框架体系 集合框架体系是由Collection.Map和 Iterator(迭代器) 实线边框的是实现类,折线边框的是抽象类,而点线边框的是接口 Collection体系 Set接口:元素无序且不 ...
- 分享图片压缩上传demo,可以选择一张或多张图片也可以拍摄照片
2016-08-05更新: 下方的代码是比较OLD的了,是通过js进行图片的剪切 旋转 再生成,效率较低. 后来又整合了一个利用native.js本地接口的压缩代码 ,链接在这 .页面中有详细的说明, ...
- MYSQL数据库基础概念
数据库的发展史 1.萌芽阶段:文件系统 使用磁盘文件来存储数据2.初级阶段:第一代数据库 出现了网状模型.层次模型的数据库3.中级阶段:第二代数据库 关系型数据库和结构化查询语言4.高级阶段:新一代数 ...
- 在Winform中屏蔽UnityWebPlayer的右键以及自带Logo解决方案整理
根据项目的需要,对已经完成的Unity三维模型以及游戏要使用Winform进行包装,也就是使用Winform做一层外壳.因此在展示Unity的时候使用到了UnityWebPlayer这个插件,对于此插 ...
- 【Linux】安装 PostgreSQL
参考: CSDN1:https://blog.csdn.net/ctwy291314/article/details/79900074 1.进入 PostgreSQL 官网的下载地址, 2.选择下面的 ...
- insmod内核模块时提示Failed to find the folder holding the modules怎么办?
答:笔者通过重新编译内核和根文件系统解决了此问题 (笔者使用的是openwrt系统) 分析: 1. ’Failed to find the folder holding the modules‘这句l ...
- error_reporting函数引起的error_log配置失效的问题
由于项目代码中大量使用了error_reporting(0);导致php.ini中的error_log失效,不记录错误日志, 导致调试起来非常不便,耗费大量的时间,所以在php.ini的配置中禁止掉e ...
- VMware虚拟机下CentOS 6.5配置网络
使用NAT模式 虚拟机网络连接使用NAT模式,物理机网络连接使用Vmnet8. 虚拟机设置里面——网络适配器,网络连接选择自定义:Vmnet8 (NAT模式) 虚拟机菜单栏—编辑—虚拟网络编辑器,选择 ...