控制台五子棋java代码 五子棋代码html
通过Java控制台程序完成一个五子棋游戏,能够实现黑白子交互下棋,程序自动判断输赢(需要完成判断输赢
import java.util.Scanner;
网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了猇亭免费建站欢迎大家使用!
public class FiveChessGame {
public static void display(char[][] arr) {// 图形显示函数
System.out.print(" ");
for (char i = '0'; i = '9'; i++)
// 打印行号0~9
System.out.print(i + " ");
for (char j = 'a'; j = 'f'; j++)
// 打印行号a~f
System.out.print(j + " ");
System.out.println();
char c = '0';
for (int i = 0; i 10; i++) {
System.out.print(c++ + " ");
for (int j = 0; j 16; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
c = 'a';
for (int i = 10; i 16; i++) {
System.out.print(c++ + " ");
for (int j = 0; j 16; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static int getNum(char c) {
int num;
switch (c) {
case '0':
num = 0;
break;
case '1':
num = 1;
break;
case '2':
num = 2;
break;
case '3':
num = 3;
break;
case '4':
num = 4;
break;
case '5':
num = 5;
break;
case '6':
num = 6;
break;
case '7':
num = 7;
break;
case '8':
num = 8;
break;
case '9':
num = 9;
break;
case 'a':
case 'A':
num = 10;
break;
case 'b':
case 'B':
num = 11;
break;
case 'c':
case 'C':
num = 12;
break;
case 'd':
case 'D':
num = 13;
break;
case 'e':
case 'E':
num = 14;
break;
case 'f':
case 'F':
num = 15;
break;
default:
System.out.println("输入有错误!");
return -1;
}
return num;
}
public static int isWinner(char c, char[][] arr, int a, int b) {
if (c == '@') {
int count = 0;
for (int i = 0; i 16; i++) {
if (arr[a][i] == '@') {
count++;
if (count = 5) {
return 1;
}
} else {
count = 0;
}
}
count = 0;
for (int i = 0; i 16; i++) {
if (arr[i][b] == '@') {
count++;
if (count = 5) {
return 1;
}
} else {
count = 0;
}
}
count = 0;
if (a = b) {
int j = 0;
for (int i = a - b; i = 15 - a + b; i++) {
if (arr[i][j] == '@') {
count++;
j++;
if (count = 5) {
return 1;
}
} else {
count = 0;
j++;
}
}
} else {
int j = 0;
for (int i = b - a; i = 15 - b + a; i++) {
if (arr[j][i] == '@') {
count++;
j++;
if (count = 5) {
return 1;
}
} else {
count = 0;
j++;
}
}
}
int j = a + b;
count = 0;
if (a + b = 15) {
for (int i = 0; i = a + b; i++) {
if (arr[i][j] == '@') {
count++;
j--;
if (count = 5) {
return 1;
}
} else {
count = 0;
j--;
}
}
} else {
j = 15;
for (int i = a + b - 15; i = 15; i++) {
if (arr[i][j] == '@') {
count++;
j--;
if (count = 5) {
return 1;
}
} else {
count = 0;
j--;
}
}
}
} else {// 判断白色胜
int count = 0;
for (int i = 0; i 16; i++) {
if (arr[a][i] == 'O') {
count++;
if (count = 5) {
return 2;
}
} else {
count = 0;
}
}
count = 0;
for (int i = 0; i 16; i++) {
if (arr[i][b] == 'O') {
count++;
if (count = 5) {
return 2;
}
} else {
count = 0;
}
}
count = 0;
if (a = b) {
int j = 0;
for (int i = a - b; i = 15 - a + b; i++) {
if (arr[i][j] == 'O') {
count++;
j++;
if (count = 5) {
return 2;
}
} else {
count = 0;
j++;
}
}
} else {
int j = 0;
for (int i = b - a; i = 15 - b + a; i++) {
if (arr[j][i] == 'O') {
count++;
j++;
if (count = 5) {
return 2;
}
} else {
count = 0;
j++;
}
}
}
int j = a + b;
count = 0;
if (a + b = 15) {
for (int i = 0; i = a + b; i++) {
if (arr[i][j] == 'O') {
count++;
j--;
if (count = 5) {
return 2;
}
} else {
count = 0;
j--;
}
}
} else {
j = 15;
for (int i = a + b - 15; i = 15; i++) {
if (arr[i][j] == 'O') {
count++;
j--;
if (count = 5) {
return 2;
}
} else {
count = 0;
j--;
}
}
}
}
return 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
char[][] arr = new char[16][16];
for (int i = 0; i 16; i++)
// 二维数组初始化
for (int j = 0; j 16; j++)
arr[i][j] = '.';
display(arr);
int i = 0;
while (true) {
if (i % 2 == 0) {
System.out.println("请黑方落子:");
String in = sc.next();
char c1 = in.charAt(0);
char c2 = in.charAt(1);
int a = getNum(c1);
int b = getNum(c2);
if (arr[a][b] != '.') {
System.out.println("该位置已经有棋子,请重新输入!");
} else {
arr[a][b] = '@';
display(arr);
int result = isWinner('@', arr, a, b);
if (result == 1) {
System.out.println("黑方获胜!");
return;
}
i++;
}
} else {
System.out.println("请白方落子:");
String in = sc.next();
char c1 = in.charAt(0);
char c2 = in.charAt(1);
int a = getNum(c1);
int b = getNum(c2);
if (arr[a][b] != '.') {
System.out.println("该位置已经有棋子,请重新输入!");
} else {
arr[a][b] = 'O';
display(arr);
int result = isWinner('O', arr, a, b);
if (result == 2) {
System.out.println("白方获胜!");
return;
}
i++;
}
}
}
}
}
二维数组做五子棋(代码)
package com.test;
import java.util.Scanner;
public class TestMain{
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
char[] name = { '空', '黑', '白' };
int count = 0;// 计数器,用来计算该谁下棋了
char[][] qi = new char[16][16];// 把棋盘情况放进二维数组中
while (true) {
int row = 0;// 初始化从控制台接收到的row和col 的index为0
int col = 0;
if (count == 0) {
print(qi, name[count], row, col);
count++;
}
if (count == 1) {
System.out.println("请黑(@)下棋(i j):");
row = console.nextInt(16)%16;// 从控制台接收到的row的index
col = console.nextInt(16)%16;// 从控制台接收到的col的index
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 下棋正确
count++;
}
if (condition.length() == 2) {// 下棋出现错误,要下的位置已经下过棋子
System.out.println("此位置已经有棋子,黑棋重新再下");
}
if (condition.length() == 3) {// 黑棋子赢
System.out.println("黑棋很给力,白棋是浮云,黑棋赢了");
break;
}
if (condition.length() == 4) {
System.out.println("白棋子赢");
break;
}
}
if (count == 2) {
System.out.println("请白(*)下棋(i j):");
row = console.nextInt(16);
col = console.nextInt(16);
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 打印棋盘
count--;
}
if (condition.length() == 2) {// 下棋出现错误,要下的位置已经下过棋子
System.out.println("此位置已经有棋子,白棋重新再下");
}
if (condition.length() == 3) {
System.out.println("黑棋子赢");
break;
}
if (condition.length() == 4) {
System.out.println("白棋很给力,黑棋是浮云,白棋赢了");
break;
}
}
}
}
/**
* @param qi是装棋盘情况
* @param name轮到谁下棋了
* row 行号 col 列号 return true-下棋正确,false-下棋错误,重新下
*/
public static String print(char[][] qi, char name, int row, int col) {
String b = "无";// 初始化下棋是正确的
String str = "0123456789111111";
// 打印下第一行
System.out.print(" ");
for (int i = 0; i str.length(); i++) {
System.out.print(str.charAt(i));
}
System.out.println();
for (int i = 0; i qi.length; i++) {
System.out.print(str.charAt(i));
for (int j = 0; j qi[i].length; j++) {
if (name == '空') { // 还没开始下棋时,棋盘默认显示是'.'
qi[i][j] = '.';
}
if ((name == '黑') (i == row) (j == col)) {
if (qi[i][j] != '.') {// 下的位置已经有棋子了,返回false
b = "有棋";
} else {
qi[i][j] = '@';// 黑方下棋后的位置显示是'@'
if (win(qi, '@', row, col)) {
b = "黑棋子";
}
}
}
if ((name == '白') (i == row) (j == col)) {
if (qi[i][j] != '.') {
b = "有棋";
} else {
qi[i][j] = '*';// 黑方下棋后的位置显示是'*'
if (win(qi, '*', row, col)) {
b = "白棋子赢";
}
}
}
System.out.print(qi[i][j]);
}
System.out.println();
}
return b;
}
public static boolean win(char[][] qi, char ch, int row, int col) {
boolean b = false;
boolean ns = n2s(qi, ch, row, col);
boolean we = w2e(qi, ch, row, col);
boolean wnes = wn2es(qi, ch, row, col);
boolean enws = en2ws(qi, ch, row, col);
b = ns || we || wnes || enws;
return b;
}
/* 从北到南方向 */
public static boolean n2s(char[][] qi, char ch, int row, int col) {
boolean win = false;
int north = 0;// begin
int south = 0;// end
while (qi[row][col] == ch) {
row--;
if (row 0) {
break;
}
}
row++;// row此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
north = row;
while (qi[row][col] == ch) {
row++;
if (row 15) {
break;
}
}
south = row;
if ((south - north) = 5) {
win = true;
}
return win;
}
/* 从西到东方向 */
public static boolean w2e(char[][] qi, char ch, int row, int col) {
boolean win = false;
int west = 0;// begin
int east = 0;// end
while (qi[row][col] == ch) {
col--;
if (col 0) {
break;
}
}
col++;// col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
west = col;
while (qi[row][col] == ch) {
col++;
if (col 15) {
break;
}
}
east = col;
if ((east - west) = 5) {
win = true;
}
return win;
}
/* 从西北到东南方向 */
public static boolean wn2es(char[][] qi, char ch, int row, int col) {
boolean win = false;
int wn = 0;// begin
int es = 0;// end
while (qi[row][col] == ch) {
row--;
col--;
if (row 0 || col 0) {
break;
}
}
row++;// row和col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
col++;
wn = row;
while (qi[row][col] == ch) {
row++;
col++;
if (row 15 || col 15) {
break;
}
}
es = row;
if ((es - wn) = 5) {
win = true;
}
return win;
}
/* 从东北到西南方向 */
public static boolean en2ws(char[][] qi, char ch, int row, int col) {
boolean win = false;
int en = 0;// begin
int ws = 0;// end
while (qi[row][col] == ch) {
row--;
col--;
if (row 0 || col 0) {
break;
}
}
row++;// row和col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
col++;
en = row;
while (qi[row][col] == ch) {
row++;
col++;
if (row 15 || col 15) {
break;
}
}
ws = row;
if ((ws - en) = 5) {
win = true;
}
return win;
}
}
求一个简单的JAVA五子棋代码!! 网上复制的别来了!
以下是现写的 实现了两人对战 自己复制后运行把 没什么难度 类名 Games
import java.util.Scanner;
public class Games {
private String board[][];
private static int SIZE = 17;
private static String roles = "A玩家";
//初始化数组
public void initBoard() {
board = new String[SIZE][SIZE];
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
// if(i==0){
// String str = "";
// str += j+" ";
// board[i][j]= str;
// }else if(i!=0j==0){
// String str = "";
// str += i+" ";
// board[i][j]= str;
// }else{
board[i][j] = "╋";
// }
}
}
}
//输出棋盘
public void printBoard() {
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
//判断所下棋子位置是否合理
public boolean isOk(int x, int y) {
boolean isRight = true;
if (x = 16 || x 1 || y = 16 | y 1) {
//System.out.println("输入错误,请从新输入");
isRight = false;
}
if (board[x][y].equals("●") || board[x][y].equals("○")) {
isRight = false;
}
return isRight;
}
//判断谁赢了
public void whoWin(Games wz) {
// 从数组挨个查找找到某个类型的棋子就从该棋子位置向右,向下,斜向右下 各查找5连续的位置看是否为5个相同的
int xlabel;// 记录第一次找到某个棋子的x坐标
int ylabel;// 记录第一次找到某个棋子的y坐标
// ●○╋
// 判断人是否赢了
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
if (board[i][j].equals("○")) {
xlabel = i;
ylabel = j;
// 横向找 x坐标不变 y坐标以此加1连成字符串
String heng = "";
if (i + 5 SIZE j + 5 SIZE) {
for (int k = j; k j + 5; k++) {
heng += board[i][k];
}
if (heng.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
// 向下判断y不变 x逐增5 连成字符串
String xia = "";
for (int l = j; l i + 5; l++) {
xia += board[l][j];
// System.out.println(xia);
}
if (xia.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
// 斜向右下判断
String youxia = "";
for (int a = 1; a = 5; a++) {
youxia += board[xlabel++][ylabel++];
}
if (youxia.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
}
}
}
}
// 判断电脑是否赢了
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
if (board[i][j].equals("●")) {
xlabel = i;
ylabel = j;
// 横向找 x坐标不变 y坐标以此加1连成字符串
String heng = "";
if (j + 5 SIZE i + 5 SIZE) {
for (int k = j; k j + 5; k++) {
heng += board[i][k];
}
if (heng.equals("●●●●●")) {
System.out.println(roles+"赢输了!您输了!");
System.exit(0);
}
// 向下判断y不变 x逐增5 连成字符串
String xia = "";
for (int l = i; l i + 5; l++) {
xia += board[l][ylabel];
// System.out.println(xia);
}
if (xia.equals("●●●●●")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
// 斜向右下判断
String youxia = "";
for (int a = 1; a = 5; a++) {
youxia += board[xlabel++][ylabel++];
}
if (youxia.equals("●●●●●")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
}
}
}
}
}
public static void main(String[] args) {
Games wz = new Games();
Scanner sc = new Scanner(System.in);
wz.initBoard();
wz.printBoard();
while (true) {
System.out.print("请"+roles+"输入X,Y坐标,必须在0-15范围内,xy以空格隔开,输入16 16结束程序");
int x = sc.nextInt();
int y = sc.nextInt();
if (x == SIZE y == SIZE) {
System.out.println("程序结束");
System.exit(0);
}
if (x SIZE || x 0 || y SIZE | y 0) {
System.out.println("输入错误,请从新输入");
continue;
}
//如果roles是A玩家 就让A玩家下棋,否则就让B玩家下棋。
if (wz.board[x][y].equals("╋")roles.equals("A玩家")) {
wz.board[x][y] = "○";
wz.printBoard();
//判断输赢
wz.whoWin(wz);
}else if(wz.board[x][y].equals("╋")roles.equals("B玩家")){
wz.board[x][y] = "●";
wz.printBoard();
//判断输赢
wz.whoWin(wz);
} else {
System.out.println("此处已经有棋子,从新输入");
continue;
}
if(roles.equals("A玩家")){
roles = "B玩家";
}else if(roles.equals("B玩家")){
roles = "A玩家";
}
}
}
}
网站标题:控制台五子棋java代码 五子棋代码html
文章路径:http://scyanting.com/article/doedpph.html