CS112Assignment2

第一份文件:Board.Java
package hw2;

/**
* @author: ENTER YOUR FIRST AND LAST NAME HERE
* @email: ENTER YOUR BU EMAIL HERE
*/
public class Board {
// 0 means empty, 1 means X, 2 means O
public static final char[] sym = {‘ ‘, ‘X’, ‘O’};
private int[][] board;
private int height, width;
private int winLength;

public Board(int h, int w, int winLen) {
height = h;
width = w;
winLength = winLen;
board = new int[height][width];
// Java initializes new array elements to 0 automatically, so the board is empty
}

public int getWidth() {
return width;
}

public void addChecker(int type, int column) {
// TODO Fill in, so that checker of a given type (1 or 2)
// is added on top of the existing stack of checkers
// in a given column
}

public boolean canAddTo(int column) {
// TODO return whether a checker can be added to the column
return true; // REMOVE THIS LINE
}

public boolean isFull() {
// TODO Return whether the entire board is filled in
// For full credit: do not scan the entire board or even just
// the column tops. Instead, add a variable to the class
// that tells you how many pieces you already have on the board
// and consult that variable
return true; // REMOVE THIS LINE
}

private boolean hasWonVertically(int type) {
// TODO Return whether there are winLength checkers of the given type
// consecutively in any single column
return true; // REMOVE THIS LINE
}

private boolean hasWonHorizontally(int type) {
// TODO Return whether there are winLength checkers of the given type
// consecutively in any single row
return true; // REMOVE THIS LINE
}

private boolean hasWonNW(int type) {
// TODO Return whether there are winLength checkers of the given type
// consecutively on a diagonal that is oriented SouthEast-NorthWest
return true; // REMOVE THIS LINE
}

private boolean hasWonNE(int type) {
// TODO Return whether there are winLength checkers of the given type
// consecutively on a diagonal that is oriented SouthWest-NorthEast
return true; // REMOVE THIS LINE
}

public boolean hasWon(int type) {
// TODO return whether there are winLength checkers
// of the given type consecutively vertically,
// horizontally, or diagonally (hint: use previous four methods)
return true; // REMOVE THIS LINE
}

// DO NOT MODIFY THIS METHOD
public String toString() {
String s = “”;
for (int i = height – 1; i >= 0; i–) {
for (int j = 0; j < width; j++) { s += "|" + sym[board[i][j]]; } s += "|\n"; } for (int j = 0; j < width; j++) { s += "--"; } s += "-\n"; for (int j = 0; j < width; j++) { s += " " + j % 10; } s += "\n"; return s; } } 第二份文件:Game.Java package hw2; public class Game { public static int play(int height, int width, int winLength, Player p1, Player p2) { Board b = new Board(height, width, winLength); System.out.println(b); // TODO: modify to quit playing and return the outcome as soon as // (a) p1 wins (return 1) // (b) p2 wins (return 2) // (c) the board fills up (return 0, because it's a tie) // Careful to make sure you work correctly regardless // of whether the size of the board is even or odd // // do NOT change, remove, or add any print statements while (true) { System.out.println("X moves next."); b.addChecker(1, p1.nextMove(b)); System.out.println(b); System.out.println("O moves next."); b.addChecker(2, p2.nextMove(b)); System.out.println(b); } } } 第三份文件:HW2Driver.Java package hw2; class HW2Driver { public static void main(String[] args) { // To test your code, experiment with other players // you implement. Of course, before experimenting with those // other players, test code as you write it. Player p1 = new HumanPlayer(1); Player p2 = new HumanPlayer(2); Game.play(6, 7, 4, p1, p2); // TODO this is where you can test what you create // and have fun--e.g., pair up a LeftmostPlayer and a // HighestColumnPlayer and see who wins. // This file is not graded. } } 第四份文件:HighestColumnPlayer.java package hw2; public class HighestColumnPlayer extends Player { // TODO implement a constructor (see HumanPlayer) // There should be no reading from or writing to the console anywhere // in this class @Override int nextMove(Board board) { // TODO this player always plays in the highest column that // is not yet full, resolving ties to the left return 0; // REMOVE THIS LINE } } 第五份文件:HumanPlayr.Java package hw2; import java.util.Scanner; public class HumanPlayer extends Player { private Scanner sc; public HumanPlayer(int t) { super(t); sc = new Scanner(System.in); } @Override int nextMove(Board board) { System.out.print(Board.sym[type] + ", your move? "); int move; do { move = sc.nextInt(); } while (move >= board.getWidth());
// TODO: currently, this checks only that the column
// is not too high. Modify to check for other
// bad inputs (column negative or full).
// Do not remove, add, or change any print statements
return move;
}
}

第六份文件:LeftmostPlayer.java

package hw2;

public class LeftmostPlayer extends Player {

// TODO implement a constructor (see HumanPlayer)
// There should be no reading from or writing to the console anywhere
// in this class

@Override
int nextMove(Board board) {
// TODO this player always plays in the leftmost column that
// is not yet full
return 0; // REMOVE THIS LINE
}

}

第七份文件:LowestColumnPlayer.java

package hw2;

public class LowestColumnPlayer extends Player {

// TODO implement a constructor (see HumanPlayer)
// There should be no reading from or writing to the console anywhere
// in this class

@Override
int nextMove(Board board) {
// TODO this player always plays in the lowest column,
// resolving ties to the left
return 0; // REMOVE THIS LINE
}

}

第八份文件: Player.java

package hw2;

// An abstract class is a class that has
// unimplemented (abstract) methods.
// These methods are implemented in sublcasses.
// An object of an abstract class can never be created — you have
// to create an object of a subclass.
// In this problem set, we partially implement one subclass for you,
// and you will complete this one and implement four more.
public abstract class Player {

protected int type; // 1 for X, 2 for O

public Player(int t) {
type = t;
}

abstract int nextMove(Board board);
}

第九份文件:RightmostPlayer.java

package hw2;

public class RightmostPlayer extends Player {

// TODO implement a constructor (see HumanPlayer)
// There should be no reading from or writing to the console anywhere
// in this class

@Override
int nextMove(Board board) {
// TODO this player always plays in the rightmost column that
// is not yet full
return 0; // REMOVE THIS LINE
}

}