Advertisement
obernardovieira

The Castle On The Hill (day 1) [SLiSW]

Aug 8th, 2015
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.66 KB | None | 0 0
  1. /*This is the bonus problem, because is a extra of the first problem. Big problem, but not so hard. I like it!*/
  2.  
  3. class TicTacToe {
  4.     var winner = -1;
  5.     var board = Array(
  6.         Array(-1,-1,-1),
  7.         Array(-1,-1,-1),
  8.         Array(-1,-1,-1)
  9.     );
  10.     println("Inicio do jogo!");
  11.  
  12.     def played(column: Int, line: Int, player: Int) : Int = {
  13.         if(board(line)(column) != -1) {
  14.             println("Already set!");
  15.             return 2;
  16.         }
  17.         else {
  18.             board(line)(column) = player;
  19.             return checkBoard();
  20.         }
  21.     }
  22.  
  23.     def seeGame() {
  24.         for(l <- 0 until 3) {
  25.             for(c <- 0 until 3) {
  26.                 if(board(l)(c) == 1) {
  27.                     print("X ");
  28.                 }
  29.                 else if(board(l)(c) == 0) {
  30.                     print("0 ");
  31.                 }
  32.                 else {
  33.                     print("_ ");
  34.                 }
  35.             }
  36.             print("\n");
  37.         }
  38.     }
  39.  
  40.     def checkBoard(): Int = {
  41.         for(i <- 0 until 3) {
  42.             if(board(i)(0) != -1) {
  43.                 if(board(i)(0) == board(i)(1) && board(i)(1) == board(i)(2)) {
  44.                     println("There is a winner! (line)");
  45.                     return 1;
  46.                 }
  47.             }
  48.         }
  49.         for(i <- 0 until 3) {
  50.             if(board(0)(i) != -1) {
  51.                 if(board(0)(i) == board(1)(i) && board(1)(i) == board(2)(i)) {
  52.                     println("There is a winner! (column)");
  53.                     return 1;
  54.                 }
  55.             }
  56.         }
  57.         if(board(0)(0) == board(1)(1) && board(1)(1) == board(2)(2) && board(0)(0) != -1) {
  58.             println("There is a winner! (Diagonal 1)");
  59.             return 1;
  60.         }
  61.         if(board(0)(2) == board(1)(1) && board(1)(1) == board(2)(0) && board(0)(2) != -1) {
  62.             println("There is a winner! (Diagonal 2)");
  63.             return 1;
  64.         }
  65.         return 0;
  66.     }
  67. }
  68.  
  69. val myTicTacToe = new TicTacToe
  70. var player = 0;
  71. var play = 0;
  72. var c = 0;
  73. var l = 0;
  74. var ttt = 0;
  75.  
  76. for(j <- 0 until 9 if ttt != 1) {
  77.     printf("Time to player %d choose!\n", player + 1);
  78.  
  79.     do {
  80.         print("Play (1) or see the game (0) ? ");
  81.         play = io.StdIn.readInt();
  82.         if(play == 0) {
  83.             //show game
  84.             myTicTacToe.seeGame();
  85.         }
  86.     }while(play == 0);
  87.  
  88.     do {
  89.         print("Write the column number: ");
  90.         c = io.StdIn.readInt();
  91.         print("Write the line number: ");
  92.         l = io.StdIn.readInt();
  93.  
  94.         ttt = myTicTacToe.played(c-1, l-1, player);
  95.  
  96.     }while(ttt == 2);
  97.  
  98.     player = player ^ 1;
  99. }
  100. println("End of the game!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement