MVC java console tic tac toe












0












$begingroup$


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.










share|improve this question







New contributor




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







$endgroup$

















    0












    $begingroup$


    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.










    share|improve this question







    New contributor




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







    $endgroup$















      0












      0








      0





      $begingroup$


      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.










      share|improve this question







      New contributor




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







      $endgroup$




      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.







      java mvc






      share|improve this question







      New contributor




      appleguy 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




      appleguy 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




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









      asked 19 mins ago









      appleguyappleguy

      1




      1




      New contributor




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





      New contributor





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






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






















          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
          });


          }
          });






          appleguy 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%2f216134%2fmvc-java-console-tic-tac-toe%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








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










          draft saved

          draft discarded


















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













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












          appleguy 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%2f216134%2fmvc-java-console-tic-tac-toe%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin