Created
September 2, 2008 09:17
-
-
Save tatey/8398 to your computer and use it in GitHub Desktop.
Solution to Lab 7 for 1005ICT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
/** | |
* Class GameBoard for Lab7 | |
* | |
* @author Tate Johnson | |
* @version 2008-09-02 | |
*/ | |
public class GameBoard { | |
private byte[][] board; | |
private final int ROWS; | |
private final int COLS; | |
public GameBoard(int row, int col) { | |
ROWS = row - 1; | |
COLS = col - 1; | |
board = new byte[row][col]; | |
for (byte[] r : board) { | |
for (int c = 0; c <= ROWS; c++) { | |
r[c] = 0; | |
} | |
} | |
} | |
public void resetBoard() { | |
for (byte[] row : board) { | |
for (int col = 0; col <= ROWS; col++) { | |
row[col] = 0; | |
} | |
} | |
} | |
public byte boardValue(int row, int col) { | |
return board[row][col]; | |
} | |
public boolean makeMove(int row, int col, byte player) { | |
if (row <= ROWS && col <= COLS && board[row][col] == 0) { | |
board[row][col] = player; | |
return true; | |
} | |
return false; | |
} | |
public void printBoard() { | |
System.out.print(" "); | |
for (int col = 0; col <= COLS; col++) { | |
System.out.print(col + " "); | |
} | |
System.out.print("\n"); | |
for (int row = 0; row <= ROWS; row++) { | |
System.out.print(row + ": "); | |
for (byte col : board[row]) { | |
System.out.print(col + " "); | |
} | |
System.out.print("\n"); | |
} | |
} | |
public void randomMove(byte player) { | |
Random random = new Random(); | |
makeMove(random.nextInt(ROWS + 1), random.nextInt(COLS + 1), player); | |
} | |
/** | |
* Plays for a specified number of games with the specified number of players. | |
* Each player takes their turn in succession and performs a random move. | |
*/ | |
public void playRandomGame(int games, byte players) { | |
byte player = players; | |
for (int game = 0; game < games; game++) { | |
if (player != 0) { | |
randomMove(player); | |
player--; | |
} | |
else { | |
player = players; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment