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
- canvas實現的五子棋程式碼例項Canvas
- 五子棋javaJava
- 人工智慧實現簡單的五子棋程式人工智慧
- Java做的五子棋Java
- 五子棋遊戲(Java)遊戲Java
- 前端談談實現五子棋前端
- Java實現五子棋對戰小遊戲【完整版】Java遊戲
- 棋盤完美覆蓋數(小規模原理實現)
- C語言實現小遊戲:五子棋C語言遊戲
- 原生JS+Canvas實現五子棋遊戲JSCanvas遊戲
- 馬踏棋盤(棧實現)
- 實現一個質量過硬五子棋
- java簡單練習-五子棋Java
- 資料結構:稀疏棋盤的實現資料結構
- 練手必備 | C 語言快速實現五子棋
- 馬踏棋盤之遞迴實現遞迴
- JavaScript五子棋效果JavaScript
- iOS-五子棋iOS
- js五子棋效果JS
- JS之五子棋遊戲JS遊戲
- 五子棋畫布-我的理解
- 五子棋AI:實現邏輯與相關背景探討(上)AI
- 五子棋AI:實現邏輯與相關背景探討(下)AI
- Python實現五子棋人機對戰的二次開發Python
- 殘缺棋盤 android實現顏色填充Android
- 16 支付功能實現
- python 五子棋原始碼Python原始碼
- 安卓開發 五子棋遊戲安卓遊戲
- 使用原生 JS 寫五子棋JS
- 結對專案——五子棋
- 結對專案 五子棋
- 基於Flutter製作的五子棋Flutter
- JDK 16:Java 16的新功能 - InfoWorldJDKJava
- poj 1321 棋盤問題 回溯 JavaJava
- Java學習筆記(七):五子棋AI演算法Java筆記AI演算法
- [2016.4][京東暑期實習筆試]三子棋筆試
- 【Demo見真章】基於HarmonyOS手機實現五子棋對戰小遊戲遊戲