Tic Tac Toe Game in Java using MVC












-1












$begingroup$


I'm working on a Tic Tac Toe game with java and need to implement it with the MVC pattern. Just wondering on what will be a good place to start? I have a working game currently only using java.



//Tic-Tac-Toe. Two player.
//Feel free to use as a starting point for 5x5 and separating into MVC.
import java.util.Scanner;

public class TicTacToe {
static final int X = 1;
static final int O = -1;

//printBoard method that prints out the current state of the
//Tic-Tac-Toe board. Does not return anything or change the board.
public static void printBoard( int matrix ){

for( int row = 0; row < matrix.length; row++ ){
for( int col = 0; col < matrix[row].length; col++ ){
// Uses the "global" constants to print out appropriate letter.
if( matrix[row][col] == X )
System.out.print("X ");
else if(matrix[row][col] == O )
System.out.print("O ");
else
System.out.print(". ");
}
// Goes to new line after printing each row
System.out.println("");
}
}

//hasWon returns true if there was a win or a cat game.
public static boolean hasWon( int matrix ){
//Variable holds the "result" of hasWon. True if a winner was found.
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];
}
//Check to see if the sum of that row was 3 or -3, a win...
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];
}
//Check to see if the sum of that column was 3 or -3, a win...
if( sum == 5 ){
System.out.println("X wins.");
retVal = true;
} else if ( sum == -5 ) {
System.out.println("O wins.");
retVal = true;
}
}

//Check for diagonal win
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][0] + matrix[1][1] + matrix[2][2] + matrix[3][3] + matrix[4][4]) == -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 static void main (String args) {
Scanner input = new Scanner(System.in);

//A 5x5 array stored as integers. X will be 1. O will be -1.
int board = new int[5][5];

while( hasWon(board) == false){

//Get the X player input and make the change if not taken.
System.out.print("X,(from 0-4) enter a row: ");
int row = input.nextInt();
System.out.print("X,(from 0-4) enter a column: ");
int col = input.nextInt();
if( board[row][col] == 0 )
board[row][col] = X;

printBoard(board);

//Check to see if X's move won the game. If so, break out of game loop
if( hasWon(board) == true )
break;

//Get the O player input and make the change if not taken.
System.out.print("O, (from 0-4) enter a row: ");
row = input.nextInt();
System.out.print("O, (from 0-4) enter a column: ");
col = input.nextInt();
if( board[row][col] == 0 )
board[row][col] = O;

printBoard(board);

}

System.out.println("Game over.");
}
}










share|improve this question







New contributor




Robert Waguespack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Welcome to Code Review! Code Review is intended to be used as a platform where people give feedback on your working and implemented code. It would be on-topic for this site to ask for a review on the code as is. Adding features or transforming your code into another pattern is not considered a valid question for code review.
    $endgroup$
    – Alex
    9 hours ago












  • $begingroup$
    Since the code is not in any form of MVC, asking for it to be made into MVC is asking for code to be written (or a tutorial), which is off-topic for Code Review.
    $endgroup$
    – 200_success
    1 hour ago
















-1












$begingroup$


I'm working on a Tic Tac Toe game with java and need to implement it with the MVC pattern. Just wondering on what will be a good place to start? I have a working game currently only using java.



//Tic-Tac-Toe. Two player.
//Feel free to use as a starting point for 5x5 and separating into MVC.
import java.util.Scanner;

public class TicTacToe {
static final int X = 1;
static final int O = -1;

//printBoard method that prints out the current state of the
//Tic-Tac-Toe board. Does not return anything or change the board.
public static void printBoard( int matrix ){

for( int row = 0; row < matrix.length; row++ ){
for( int col = 0; col < matrix[row].length; col++ ){
// Uses the "global" constants to print out appropriate letter.
if( matrix[row][col] == X )
System.out.print("X ");
else if(matrix[row][col] == O )
System.out.print("O ");
else
System.out.print(". ");
}
// Goes to new line after printing each row
System.out.println("");
}
}

//hasWon returns true if there was a win or a cat game.
public static boolean hasWon( int matrix ){
//Variable holds the "result" of hasWon. True if a winner was found.
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];
}
//Check to see if the sum of that row was 3 or -3, a win...
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];
}
//Check to see if the sum of that column was 3 or -3, a win...
if( sum == 5 ){
System.out.println("X wins.");
retVal = true;
} else if ( sum == -5 ) {
System.out.println("O wins.");
retVal = true;
}
}

//Check for diagonal win
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][0] + matrix[1][1] + matrix[2][2] + matrix[3][3] + matrix[4][4]) == -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 static void main (String args) {
Scanner input = new Scanner(System.in);

//A 5x5 array stored as integers. X will be 1. O will be -1.
int board = new int[5][5];

while( hasWon(board) == false){

//Get the X player input and make the change if not taken.
System.out.print("X,(from 0-4) enter a row: ");
int row = input.nextInt();
System.out.print("X,(from 0-4) enter a column: ");
int col = input.nextInt();
if( board[row][col] == 0 )
board[row][col] = X;

printBoard(board);

//Check to see if X's move won the game. If so, break out of game loop
if( hasWon(board) == true )
break;

//Get the O player input and make the change if not taken.
System.out.print("O, (from 0-4) enter a row: ");
row = input.nextInt();
System.out.print("O, (from 0-4) enter a column: ");
col = input.nextInt();
if( board[row][col] == 0 )
board[row][col] = O;

printBoard(board);

}

System.out.println("Game over.");
}
}










share|improve this question







New contributor




Robert Waguespack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Welcome to Code Review! Code Review is intended to be used as a platform where people give feedback on your working and implemented code. It would be on-topic for this site to ask for a review on the code as is. Adding features or transforming your code into another pattern is not considered a valid question for code review.
    $endgroup$
    – Alex
    9 hours ago












  • $begingroup$
    Since the code is not in any form of MVC, asking for it to be made into MVC is asking for code to be written (or a tutorial), which is off-topic for Code Review.
    $endgroup$
    – 200_success
    1 hour ago














-1












-1








-1





$begingroup$


I'm working on a Tic Tac Toe game with java and need to implement it with the MVC pattern. Just wondering on what will be a good place to start? I have a working game currently only using java.



//Tic-Tac-Toe. Two player.
//Feel free to use as a starting point for 5x5 and separating into MVC.
import java.util.Scanner;

public class TicTacToe {
static final int X = 1;
static final int O = -1;

//printBoard method that prints out the current state of the
//Tic-Tac-Toe board. Does not return anything or change the board.
public static void printBoard( int matrix ){

for( int row = 0; row < matrix.length; row++ ){
for( int col = 0; col < matrix[row].length; col++ ){
// Uses the "global" constants to print out appropriate letter.
if( matrix[row][col] == X )
System.out.print("X ");
else if(matrix[row][col] == O )
System.out.print("O ");
else
System.out.print(". ");
}
// Goes to new line after printing each row
System.out.println("");
}
}

//hasWon returns true if there was a win or a cat game.
public static boolean hasWon( int matrix ){
//Variable holds the "result" of hasWon. True if a winner was found.
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];
}
//Check to see if the sum of that row was 3 or -3, a win...
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];
}
//Check to see if the sum of that column was 3 or -3, a win...
if( sum == 5 ){
System.out.println("X wins.");
retVal = true;
} else if ( sum == -5 ) {
System.out.println("O wins.");
retVal = true;
}
}

//Check for diagonal win
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][0] + matrix[1][1] + matrix[2][2] + matrix[3][3] + matrix[4][4]) == -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 static void main (String args) {
Scanner input = new Scanner(System.in);

//A 5x5 array stored as integers. X will be 1. O will be -1.
int board = new int[5][5];

while( hasWon(board) == false){

//Get the X player input and make the change if not taken.
System.out.print("X,(from 0-4) enter a row: ");
int row = input.nextInt();
System.out.print("X,(from 0-4) enter a column: ");
int col = input.nextInt();
if( board[row][col] == 0 )
board[row][col] = X;

printBoard(board);

//Check to see if X's move won the game. If so, break out of game loop
if( hasWon(board) == true )
break;

//Get the O player input and make the change if not taken.
System.out.print("O, (from 0-4) enter a row: ");
row = input.nextInt();
System.out.print("O, (from 0-4) enter a column: ");
col = input.nextInt();
if( board[row][col] == 0 )
board[row][col] = O;

printBoard(board);

}

System.out.println("Game over.");
}
}










share|improve this question







New contributor




Robert Waguespack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I'm working on a Tic Tac Toe game with java and need to implement it with the MVC pattern. Just wondering on what will be a good place to start? I have a working game currently only using java.



//Tic-Tac-Toe. Two player.
//Feel free to use as a starting point for 5x5 and separating into MVC.
import java.util.Scanner;

public class TicTacToe {
static final int X = 1;
static final int O = -1;

//printBoard method that prints out the current state of the
//Tic-Tac-Toe board. Does not return anything or change the board.
public static void printBoard( int matrix ){

for( int row = 0; row < matrix.length; row++ ){
for( int col = 0; col < matrix[row].length; col++ ){
// Uses the "global" constants to print out appropriate letter.
if( matrix[row][col] == X )
System.out.print("X ");
else if(matrix[row][col] == O )
System.out.print("O ");
else
System.out.print(". ");
}
// Goes to new line after printing each row
System.out.println("");
}
}

//hasWon returns true if there was a win or a cat game.
public static boolean hasWon( int matrix ){
//Variable holds the "result" of hasWon. True if a winner was found.
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];
}
//Check to see if the sum of that row was 3 or -3, a win...
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];
}
//Check to see if the sum of that column was 3 or -3, a win...
if( sum == 5 ){
System.out.println("X wins.");
retVal = true;
} else if ( sum == -5 ) {
System.out.println("O wins.");
retVal = true;
}
}

//Check for diagonal win
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][0] + matrix[1][1] + matrix[2][2] + matrix[3][3] + matrix[4][4]) == -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 static void main (String args) {
Scanner input = new Scanner(System.in);

//A 5x5 array stored as integers. X will be 1. O will be -1.
int board = new int[5][5];

while( hasWon(board) == false){

//Get the X player input and make the change if not taken.
System.out.print("X,(from 0-4) enter a row: ");
int row = input.nextInt();
System.out.print("X,(from 0-4) enter a column: ");
int col = input.nextInt();
if( board[row][col] == 0 )
board[row][col] = X;

printBoard(board);

//Check to see if X's move won the game. If so, break out of game loop
if( hasWon(board) == true )
break;

//Get the O player input and make the change if not taken.
System.out.print("O, (from 0-4) enter a row: ");
row = input.nextInt();
System.out.print("O, (from 0-4) enter a column: ");
col = input.nextInt();
if( board[row][col] == 0 )
board[row][col] = O;

printBoard(board);

}

System.out.println("Game over.");
}
}







java mvc






share|improve this question







New contributor




Robert Waguespack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Robert Waguespack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Robert Waguespack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 9 hours ago









Robert WaguespackRobert Waguespack

4




4




New contributor




Robert Waguespack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Robert Waguespack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Robert Waguespack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    Welcome to Code Review! Code Review is intended to be used as a platform where people give feedback on your working and implemented code. It would be on-topic for this site to ask for a review on the code as is. Adding features or transforming your code into another pattern is not considered a valid question for code review.
    $endgroup$
    – Alex
    9 hours ago












  • $begingroup$
    Since the code is not in any form of MVC, asking for it to be made into MVC is asking for code to be written (or a tutorial), which is off-topic for Code Review.
    $endgroup$
    – 200_success
    1 hour ago


















  • $begingroup$
    Welcome to Code Review! Code Review is intended to be used as a platform where people give feedback on your working and implemented code. It would be on-topic for this site to ask for a review on the code as is. Adding features or transforming your code into another pattern is not considered a valid question for code review.
    $endgroup$
    – Alex
    9 hours ago












  • $begingroup$
    Since the code is not in any form of MVC, asking for it to be made into MVC is asking for code to be written (or a tutorial), which is off-topic for Code Review.
    $endgroup$
    – 200_success
    1 hour ago
















$begingroup$
Welcome to Code Review! Code Review is intended to be used as a platform where people give feedback on your working and implemented code. It would be on-topic for this site to ask for a review on the code as is. Adding features or transforming your code into another pattern is not considered a valid question for code review.
$endgroup$
– Alex
9 hours ago






$begingroup$
Welcome to Code Review! Code Review is intended to be used as a platform where people give feedback on your working and implemented code. It would be on-topic for this site to ask for a review on the code as is. Adding features or transforming your code into another pattern is not considered a valid question for code review.
$endgroup$
– Alex
9 hours ago














$begingroup$
Since the code is not in any form of MVC, asking for it to be made into MVC is asking for code to be written (or a tutorial), which is off-topic for Code Review.
$endgroup$
– 200_success
1 hour ago




$begingroup$
Since the code is not in any form of MVC, asking for it to be made into MVC is asking for code to be written (or a tutorial), which is off-topic for Code Review.
$endgroup$
– 200_success
1 hour ago










0






active

oldest

votes












Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Robert Waguespack is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216449%2ftic-tac-toe-game-in-java-using-mvc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








Robert Waguespack is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Robert Waguespack is a new contributor. Be nice, and check out our Code of Conduct.













Robert Waguespack is a new contributor. Be nice, and check out our Code of Conduct.












Robert Waguespack is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216449%2ftic-tac-toe-game-in-java-using-mvc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀