Java小项目-Domineering demo

玩家:2人,一位玩家在水平方向上玩游戏,另一位玩家在垂直方向上玩游戏。
目标:成为最后一个能够合法移动多米诺骨牌的人
棋盘:棋盘是8×8四方格棋盘,就像国际象棋或者西洋跳棋一样。它最初是空的。
玩法:在一局中,玩家把一张多米诺骨牌放在棋盘上,占据相邻的两个方格。一位玩家水平地(由东向西—)放置他的多米诺骨牌,另一外玩家垂直地(由北向南)放置他的多米诺骨牌。忽略多米诺骨牌上的点,但是一张多米诺骨牌不能与以前玩的任何多米诺骨牌重叠。

Board Class

import org.omg.CORBA.PUBLIC_MEMBER;

/**
 * Created by root on 16-2-23.
 */
public class Board {
    public static final boolean HORIZONTAL = false;
    public static final boolean VERTICAL = true;
    int rowOffset;
    int columnOffset;
    private boolean[][] squares;//棋盘

    public Board() {
        squares = new boolean[8][8];//构造方法的同时初始化
    }

    public String toString() {//打印棋盘
        String result = " 1 2 3 4 5 6 7 8";//列标索引
        for (int row = 0; row < 8; row++) {
            result += "\n" + (row+1);//行标索引
            for (int column = 0; column < 8; column++) {
                if (!squares[row][column]) {//true输出点
                    result += ". ";
                } else {//false 输出  #
                    result += "# ";
                }
            }
        }
        return result;
    }

    public boolean hasLegalMoveFor(boolean player) {//HORIZONTAL=FALSE;palyer是否可以继续放牌?
        if (player == HORIZONTAL) {
            rowOffset = 0;
            columnOffset = 1;//水平放2张牌
        } else {
            rowOffset = 1;
            columnOffset = 0;//垂直放2张牌
        }
        for (int row = 0; row < 8 - rowOffset; row++) {
            for (int column = 0; column < 8 - columnOffset; column++) {
                if ((squares[row][column] == false) && (squares[row + rowOffset][column + columnOffset] == false)) {
                    return true;//存在合法的位置
                }
            }
        }
        return false;//不存在合法位置
    }

    public boolean playAt(int row, int column, boolean player) {//返回能否在指定位置放牌 可以返回true并放牌
        if (player) {//Vertical                                  //否则返回false并不执行操作
            if ((squares[row][column] == false) &&
                    (squares[row + 1][column] == false)) {
                squares[row][column] = true;
                squares[row + 1][column] = true;
                return true;
            } else {
                return false;
            }
        }
          else {
            if ((squares[row][column] == false) && (squares[row][column + 1] == false)) {
                squares[row][column] = true;
                squares[row][column + 1] = true;
                return true;
            } else {
                return false;
            }

        }
    }
}

Main Class

/**
 * Created by root on 16-2-23.
 */
import java.util.Scanner;
public class Main {
    static final boolean HORIZONTAL=false;
    static final boolean VERTICAL=true;
    public static final java.util.Scanner INPUT=new java.util.Scanner(System.in);//获取用户输入
    Board board;
    public static void main(String[]args){
        Main main=new Main();//静态方法使用本类方法先构造
        main.play();
    }
    public void play(){//主要逻辑部分
        System.out.println("Welcome to play Domineer");

        boolean player=HORIZONTAL;

        while(true){
            if(board.hasLegalMoveFor(player)==false){
                if(player==HORIZONTAL){System.out.print("游戏结束 玩家HORIZONTAL输了");break;}
                else {
                    System.out.print("游戏结束 玩家HORIZONTAL输了");break;//有一方不能合法放牌则终止循环
                }
            }
            System.out.print(board.toString());
            if(player==HORIZONTAL){System.out.println("\n player:"+"HORIZONTAL");}
            else System.out.println("\n player: "+"VERTICAL");
            System.out.println("输入行索引");
           int row= INPUT.nextInt();
            System.out.print("输入列索引");
            int column=INPUT.nextInt();
          if(board.playAt(row-1,column-1,player));
            else {
              System.out.println("输入有误 !\n  请重新输入\n ");continue;
          }
            player=!player;//交换玩家

        }
    }
    public Main(){
        board=new Board();//在静态方法中调用

    }
}
posted @ 2016-02-22 21:56  Salaku  阅读(418)  评论(0编辑  收藏  举报