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 新酷产品第一时间免费试玩,还有众多优质达人分享 ...
随机推荐
- stark项目流程
1 创建Django项目 2 创建app python manage.py startapp app名 3 文件配置 app配置 静态文件配置 创建文件夹,下载bootstrap,jquery 归档文 ...
- 【概率论】3-8:随机变量函数(Functions of a Random Variable)
title: [概率论]3-8:随机变量函数(Functions of a Random Variable) categories: Mathematic Probability keywords: ...
- open suse tumbleweed安装记录
zypper install imagewriter cmake blender fontforge gimp digikam inkscape kontact pitivi smplayer si ...
- servlet容器:jetty,Tomcat,JBoss
一.几款servlet容器对比:jetty,Tomcat,JBoss 二.JBOSS相关问题解决 1.JBOSS下载安装 2.处理jboss-as-7.1.1.Final与jdk1.8及1.8以上版本 ...
- java并发编程--第一章并发编程的挑战
一.java并发编程的挑战 并发编程需要注意的问题: 并发编程的目的是让程序运行的更快,然而并不是启动更多的线程就能让程序最大限度的并发执行.若希望通过多线程并发让程序执行的更快,会受到如下问题的挑战 ...
- Python实现进度条的效果
from itertools import cycle from time import sleep for frame in cycle(r'-\|/-\|/'): print('\r', fram ...
- 删除顺序表L中下标为p(0<=p<=length-1)的元素,成功返回1,不成功返回0,并将删除元素的值赋给e
原创:转载请注明出处. [天勤2-2]删除顺序表L中下标为p(0<=p<=length-1)的元素,成功返回1,不成功返回0,并将删除元素的值赋给e 代码: //删除顺序表L中下标为p(0 ...
- Python动态展示遗传算法求解TSP旅行商问题(转载)
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/jiang425776024/articl ...
- Python 自学笔记(五)
1.布尔值 1-1.概念 定义计算机中的逻辑判断,只有两种结果,True和False. if,while后面的判断条件就是布尔值,只有条件为True的时候才执行. 1-2.数值比较 1-3.数值运算 ...
- 如何在linux用户空间程序中打印时间戳?
1. 使用clock_gettime接口即可 2. clock_gettime的使用方法: 2.1 定义一个结构体 struct timespec ts; 2.2 调用clock_gettime获取当 ...