public class Model extends TicTacToe
{
public static boolean hasWon( int [][] matrix )
{
boolean retVal = false;
//Check for horizontal win
for( int row = 0; row < matrix.length; row++ ){
int sum = 0;
for( int col = 0; col < matrix[0].length; col++ ){
sum += matrix[row][col];
}
if( sum == 5 ){
System.out.println("X wins.");
retVal = true;
} else if ( sum == -5 ) {
System.out.println("O wins.");
retVal = true;
}
}
//Check for vertical win
for( int col = 0; col < matrix[0].length; col++ ){
int sum = 0;
for( int row = 0; row < matrix.length; row++ ){
sum += matrix[row][col];
}
if( sum == 5 ){
System.out.println("X wins.");
retVal = true;
} else if ( sum == -5 ) {
System.out.println("O wins.");
retVal = true;
}
}
if( (matrix[0][0] + matrix[1][1] + matrix[2][2]+matrix[3][3]+matrix[4][4]) == 5 ){
System.out.println("X wins.");
retVal = true;
} else if ( (matrix[0][4] + matrix[1][3] + matrix[2][2]+matrix[3][1]+matrix[4][0]) == -5 ) {
System.out.println("O wins.");
retVal = true;
}
if( (matrix[0][4] + matrix[1][3] + matrix[2][2]+matrix[3][1]+matrix[4][0]) == 5){
System.out.println("X wins.");
retVal = true;
} else if ( (matrix[0][4] + matrix[1][3] + matrix[2][2]+matrix[3][1]+matrix[4][0]) == -5 ) {
System.out.println("O wins.");
retVal = true;
}
//Check for cat game
boolean foundSpace = false;
for( int row = 0; row < matrix.length; row++ ){
for( int col = 0; col < matrix[0].length; col++ ){
if( matrix[row][col] == 0 )
foundSpace = true;
}
}
if( foundSpace == false ){
System.out.println("Ends in tie.");
retVal = true;
}
return retVal;
}
}
public class View extends TicTacToe
{ public static void printBoard( int [][] matrix ){
for( int row = 0; row < matrix.length; row++ ){
for( int col = 0; col < matrix[row].length; col++ ){
if( matrix[row][col] == X )
System.out.print("X ");
else if(matrix[row][col] == O )
System.out.print("O ");
else
System.out.print(". ");
}
System.out.println("");
}
}
}
import java.util.Scanner;
public class Controller extends TicTacToe
{
Model model;
View view;
public Controller (Model model, View view) {
this.model= model;
this.view= view;
}
public static void main (String [] args)
{
Model model= new Model();
View view= new View();
Scanner input = new Scanner(System.in);
int [][] board = new int[5][5];
while( model.hasWon(board) == false){
//Get the X player input and make the change if not taken.
System.out.print("X, enter row: ");
int row = input.nextInt();
System.out.print("X, enter column: ");
int col = input.nextInt();
if(col<=4 && col>= 0)
{
if( board[row][col] == 0 )
board[row][col] = X;
view.printBoard(board);
}
else
System.out.println("Invalid Input");
//Check to see if X's move won the game. If so, break out of game loop
if( model.hasWon(board) == true )
break;
//Get the O player input and make the change if not taken.
System.out.print("O, enter row: ");
row = input.nextInt();
System.out.print("O, enter column: ");
col = input.nextInt();
if(col<=4 && col>= 0)
{
if( board[row][col] == 0 )
board[row][col] = O;
view.printBoard(board);
}
else
System.out.println("Invalid Input");
}
System.out.println("Game over.");
}
}
public class TicTacToe
{
static final int X =1;
static final int O = -1;
}
This is what I have so far to make a Java console MVC-based tic-tac-toe can anyone help me to see if I am right? Any help appreciated.
interface
s andenum
s before MVC. \$\endgroup\$