java programming

profilebishnu
WinOptions.docx

/**

* COSC 1436 - Programming Fundamentals I

* El Centro College

* Professor Keith Smelser

*

* Final Project support file.

* This file does not need to change or be included in the submission.

*

*/

import java.util.ArrayList;

public class WinOptions {

// Directions to Win

enum WIN_WAY {

ROW1, ROW2, ROW3, COL1, COL2, COL3, DIAG_DOWN, DIAG_UP

}

// State of Board Position

enum STATE {

PREFERRED, AVAILABLE, FULL, BLOCKED, OPEN

}

// Method level variables

private int position;

private STATE winState;

// This constructor loads from the GUI 2d slots by winning options ( rows,columns, diagonals)

public WinOptions(final Character[][] TTB, WIN_WAY theWay, char player) {

ArrayList<Character> slots = new ArrayList<Character>(3);

switch (theWay) {

case ROW1: {

slots.add(TTB[0][0]);

slots.add(TTB[0][1]);

slots.add(TTB[0][2]);

break;

}

case ROW2: {

slots.add(TTB[1][0]);

slots.add(TTB[1][1]);

slots.add(TTB[1][2]);

break;

}

case ROW3: {

slots.add(TTB[2][0]);

slots.add(TTB[2][1]);

slots.add(TTB[2][2]);

break;

}

case COL1: {

slots.add(TTB[0][0]);

slots.add(TTB[1][0]);

slots.add(TTB[2][0]);

break;

}

case COL2: {

slots.add(TTB[0][1]);

slots.add(TTB[1][1]);

slots.add(TTB[2][1]);

break;

}

case COL3: {

slots.add(TTB[0][2]);

slots.add(TTB[1][2]);

slots.add(TTB[2][2]);

break;

}

case DIAG_DOWN: {

slots.add(TTB[0][0]);

slots.add(TTB[1][1]);

slots.add(TTB[2][2]);

break;

}

case DIAG_UP: {

slots.add(TTB[0][2]);

slots.add(TTB[1][1]);

slots.add(TTB[2][0]);

break;

}

}

winState = checkSlot(slots, player);

if (winState.equals(STATE.AVAILABLE) || winState.equals(STATE.PREFERRED) || winState.equals(STATE.OPEN)) {

setPosition(slots, theWay);

}

}

// Sets the method level variable - position

private void setPosition(ArrayList<Character> slots, WIN_WAY theWay) {

switch (theWay) {

case ROW1: {

if (slots.get(0) == null) {

position = 0;

} else if (slots.get(1) == null) {

position = 1;

} else if (slots.get(2) == null) {

position = 2;

}

break;

}

case ROW2: {

if (slots.get(0) == null) {

position = 3;

} else if (slots.get(1) == null) {

position = 4;

} else if (slots.get(2) == null) {

position = 5;

}

break;

}

case ROW3: {

if (slots.get(0) == null) {

position = 6;

} else if (slots.get(1) == null) {

position = 7;

} else if (slots.get(2) == null) {

position = 8;

}

break;

}

case COL1: {

if (slots.get(0) == null) {

position = 0;

} else if (slots.get(1) == null) {

position = 3;

} else if (slots.get(2) == null) {

position = 6;

}

break;

}

case COL2: {

if (slots.get(0) == null) {

position = 1;

} else if (slots.get(1) == null) {

position = 4;

} else if (slots.get(2) == null) {

position = 7;

}

break;

}

case COL3: {

if (slots.get(0) == null) {

position = 2;

} else if (slots.get(1) == null) {

position = 5;

} else if (slots.get(2) == null) {

position = 8;

}

break;

}

case DIAG_DOWN: {

if (slots.get(0) == null) {

position = 0;

} else if (slots.get(1) == null) {

position = 4;

} else if (slots.get(2) == null) {

position = 8;

}

break;

}

case DIAG_UP: {

if (slots.get(0) == null) {

position = 2;

} else if (slots.get(1) == null) {

position = 4;

} else if (slots.get(2) == null) {

position = 6;

}

break;

}

}

//System.out.println("Position set at " + position);

}

// Returns a STATE enum to indicate the state of a slot to by the 3 winning options ( rows,columns, diagonals)

private STATE checkSlot(ArrayList<Character> slots, char player) {

if (slots.get(0) != null && slots.get(0) != player) {

return STATE.BLOCKED;

}

if (slots.get(1) != null && slots.get(1) != player) {

return STATE.BLOCKED;

}

if (slots.get(2) != null && slots.get(2) != player) {

return STATE.BLOCKED;

}

STATE reply = STATE.FULL;

int countFound = 0;

for (Character slot : slots) {

if (slot == null) {

countFound++;

}

}

if (countFound == 3) {

reply = STATE.OPEN;

} else if (countFound == 2) {

reply = STATE.AVAILABLE;

} else if (countFound == 1) {

reply = STATE.PREFERRED;

}

return reply;

}

/**

* @return the position

*/

public int getPosition() {

return position;

}

/**

* @return the winState

*/

public STATE getWinState() {

return winState;

}

}