很久以前写的一个五子棋游戏,这两天因为需要拿出来重新改了一下,代码如下:
Wuziqi.java
Game.java
Point.java
InputUtil.java
MainClass.java
public class Wuziqi {
private static final int ROW=9;
private static final int COLUMN=9;
private int[][] bord;
private boolean turn;
public Wuziqi() {
init();
}
public void init(){
bord = new int[ROW][COLUMN];
turn = true;
}
public void printBord(){
System.out.println("****************************************************");
System.out.print(" ");
for(int i=0;i<9;i++){
System.out.print((char)('a'+i)+" ");
}
System.out.println();
for(int i=0;i<9;i++){
System.out.print(i);
for(int j=0;j<9;j++){
if(bord[i][j] == 0){
System.out.print("_ ");
}else if(bord[i][j] == 1){
System.out.print("X ");
}else if(bord[i][j] == -1){
System.out.print("O ");
}
}
System.out.println();
}
System.out.println("****************************************************");
}
public Point getInput(){
int x;
int y;
while(true){
System.out.print("input the position:");
Scanner scan = new Scanner(System.in);
String input = scan.next();
x = Integer.valueOf(input.substring(0, 1));
y = Integer.valueOf(input.charAt(1)-'a');
System.out.println(x+" "+ y);
if(validation(x,y,0)){
break;
}else{
System.out.println("your input is invalidation!try again");
}
}
return new Point(x,y);
}
public void playChess(){
init();
while(true){
printBord();
Point p = null;
if(turn){
System.out.println("请白棋下,");;
p = getInput();
bord[p.getX()][p.getY()] = 1;
}else{
System.out.println("请黑棋下,");;
p = getInput();
bord[p.getX()][p.getY()] = -1;
}
if(isWin(p.getX(), p.getY(), bord[p.getX()][p.getY()])){
printBord();
announceWinner();
init();
break;
}
turn = !turn;
}
}
public void announceWinner(){
if(turn){
System.out.println("白棋赢了...");
}else{
System.out.println("黑棋赢了...");
}
}
public boolean validation(int x, int y, int type){
if (x < 0 || x >= ROW || y < 0 || y >= COLUMN) {
return false;
}
if (bord[x][y] == type) {
return true;
}
return false;
}
public boolean isWin(int x, int y, int type){
//八个方向搜索
// 往 1 8方向
int count = 0;
int k = 0;
int l = 0;
// 往 1 方向算
k = x;
l = y;
for (int i = 0; i < 5; i++) {
if (validation(k, l, type)) {
count++;
} else {
break;
}
k--;
l--;
}
// 往8方向算
k = x;
l = y;
for (int i = 0; i < 5; i++) {
if (validation(k, l, type)) {
count++;
} else {
break;
}
k++;
l++;
}
if (count >= 6 || count <=-6) {
return true;
}
// 往 2 方向算
count=0;
k = x;
l = y;
for (int i = 0; i < 5; i++) {
if (validation(k, l, type)) {
count++;
} else {
break;
}
k--;
}
// 往7方向算
k = x;
l = y;
for (int i = 0; i < 5; i++) {
if (validation(k, l, type)) {
count++;
} else {
break;
}
k++;
}
if (count >= 6 || count <=-6) {
return true;
}
// 往3 方向算
count=0;
k = x;
l = y;
for (int i = 0; i < 5; i++) {
if (validation(k, l, type)) {
count++;
} else {
break;
}
k--;
l++;
}
// 往6方向算
k = x;
l = y;
for (int i = 0; i < 5; i++) {
if (validation(k, l, type)) {
count++;
} else {
break;
}
k--;
l++;
}
if (count >= 6 || count <=-6) {
return true;
}
// 往 4方向算
count=0;
k = x;
l = y;
for (int i = 0; i < 5; i++) {
if (validation(k, l, type)) {
count++;
} else {
break;
}
l++;
}
// 往5方向算
k = x;
l = y;
for (int i = 0; i < 5; i++) {
if (validation(k, l, type)) {
count++;
} else {
break;
}
l--;
}
if (count >= 6 || count <=-6) {
return true;
}
return false;
}
}
public class Game {
private Wuziqi wuziqi;
public Game(){
this.wuziqi = new Wuziqi();
}
public void start() {
int choice = 0;
while (true) {
instructions();
System.out.print("请选择:");
choice = InputUtil.getInt();
if (choice == 0) {
System.out.println("已退出");
System.exit(0);
} else if (choice == 1) {
wuziqi.playChess();
} else if (choice == 2) {
help();
} else {
System.out.println("error!");
}
}
}
public void instructions() {
System.out.println("欢迎进入五子棋游戏:0-退出,1-开始,2-帮助");
}
public void help() {
System.out.println("help information!");
}
}
/*
* 代表棋盘上的一个点
*/
public class Point {
private int x;
private int y;
public Point(){
}
public Point(int x,int y){
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
import java.util.Scanner;
public class InputUtil {
private static Scanner scan = new Scanner(System.in);
public static int getInt(){
return scan.nextInt();
}
public static String getString(){
return scan.next();
}
}
public class MainClass {
public static void main(String[] args) {
new Game().start();
}
}