16×16大小棋盤的五子棋小程式 Java實現
16×16大小棋盤的五子棋小程式 Java實現
記錄一下每天的學習。
import java.util.Random;
import java.util.Scanner;
/**
* @author hxf
* * @date 2020/12/7
*/
public class Test {
public static void main(String[] args) {
Chessboard chessboard = new Chessboard();
chessboard.offensive();
while (true){
chessboard.chess();
chessboard.show();
if (chessboard.check()){
System.out.println(chessboard.player+"號選手獲勝");
break;
}
chessboard.exchange();
}
}
}
class Chessboard{
int size = 16;
int[][] chessBoard = new int[size+1][size+1];
int player1 = 1;
int player2 = 2;
int player;
int row;
int column;
int number = 5;
int count = 0;
Scanner in = new Scanner(System.in);
Chessboard(){
for (int i = 0; i < chessBoard.length; i++){
chessBoard[i][0] = i;
chessBoard[0][i] = i;
}
}
void show(){
for (int[] row: chessBoard) {
for (int column: row) {
System.out.print(column+"\t");
}
System.out.println();
}
}
void offensive(){
Random random = new Random();
if (random.nextBoolean()){
player = player1;
}else {
player = player2;
}
System.out.println(player+"號先行");
}
void chess(){
System.out.println(player+"號請落子:(先輸入行,在輸入列,空格隔開,數值為1-16)");
row = in.nextInt();
column = in.nextInt();
chessBoard[row][column] = player;
}
void exchange(){
if (player == player2){
player = player1;
}else{
player = player2;
}
}
boolean checkRow(){
count = 0;
for (int i = Math.max(1, column-number+1); i <= Math.min(size, column+number-1); i++){
if (chessBoard[row][i] == player){
count++;
if (count == number){
return true;
}
}else{
count = 0;
}
}
return false;
}
boolean checkColumn(){
count = 0;
for (int i = Math.max(1, row-number+1); i <= Math.min(size, row+number-1); i++){
if (chessBoard[i][column] == player){
count++;
if (count == number){
return true;
}
}else{
count = 0;
}
}
return false;
}
boolean checkLeftward(){
count = 0;
int temp;
for (int i = Math.max(1, row-number+1); i <= Math.min(size, row+number-1); i++){
temp = column - row + i;
if (temp <1 || temp > 16){
continue;
}
if (chessBoard[i][temp] == player){
count++;
if (count == number){
return true;
}
}else{
count = 0;
}
}
return false;
}
boolean checkRightward(){
count = 0;
int temp;
for (int i = Math.max(1, row-number+1); i <= Math.min(size, row+number-1); i++){
temp = column + row - i;
if (temp <1 || temp > 16){
continue;
}
if (chessBoard[i][temp] == player){
count++;
if (count == number){
return true;
}
}else{
count = 0;
}
}
return false;
}
boolean check(){
return checkRow() || checkColumn() || checkLeftward() || checkRightward();
}
}
相關文章
- Java實現五子棋對戰小遊戲【完整版】Java遊戲
- Java簡單實現無介面五子棋Java
- C語言實現小遊戲:五子棋C語言遊戲
- 五子棋javaJava
- 人工智慧實現簡單的五子棋程式人工智慧
- 前端談談實現五子棋前端
- 五子棋遊戲(Java)遊戲Java
- JDK 16:Java 16的新功能 - InfoWorldJDKJava
- 棋盤完美覆蓋數(小規模原理實現)
- 實現一個質量過硬五子棋
- java簡單練習-五子棋Java
- C語言五子棋小遊戲C語言遊戲
- 【Demo見真章】基於HarmonyOS手機實現五子棋對戰小遊戲遊戲
- 排名Top 16的Java實用類庫Java
- PyGame每日一練——五子棋小遊戲GAM遊戲
- JAVA 學習(一):16進位制字串自增的實現Java字串
- 資料結構:稀疏棋盤的實現資料結構
- 練手必備 | C 語言快速實現五子棋
- JDK/Java 16 GAJDKJava
- 基於 Blazor 開發五子棋⚫⚪小遊戲Blazor遊戲
- js五子棋效果JS
- Python實現五子棋人機對戰的二次開發Python
- 逆水寒五子棋怎麼玩?逆水寒五子棋玩法攻略
- JS實現 CRC加密 ModbusCRC16JS加密
- 2018-10-16 java學習小專案合Java
- “16*16逐行式順向陰碼”轉“16*16列行式逆向陰碼”字模C程式碼C程式
- 排名前16的Java工具類Java
- Java 16釋出可用Java
- 五子棋AI:實現邏輯與相關背景探討(上)AI
- 五子棋AI:實現邏輯與相關背景探討(下)AI
- python 五子棋原始碼Python原始碼
- 16_簡單計算器實現
- 16種方法實現水平居中垂直居中
- 手拉手教你實現一門程式語言 Enkel, 系列 16
- 直播16小時實現31萬交易額,小眾新茶飲品牌借短影片“出圈”
- 基於Flutter製作的五子棋Flutter
- Java 開發筆記16Java筆記
- Java 16 新特性:record類Java