Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*This is the bonus problem, because is a extra of the first problem. Big problem, but not so hard. I like it!*/
- class TicTacToe {
- var winner = -1;
- var board = Array(
- Array(-1,-1,-1),
- Array(-1,-1,-1),
- Array(-1,-1,-1)
- );
- println("Inicio do jogo!");
- def played(column: Int, line: Int, player: Int) : Int = {
- if(board(line)(column) != -1) {
- println("Already set!");
- return 2;
- }
- else {
- board(line)(column) = player;
- return checkBoard();
- }
- }
- def seeGame() {
- for(l <- 0 until 3) {
- for(c <- 0 until 3) {
- if(board(l)(c) == 1) {
- print("X ");
- }
- else if(board(l)(c) == 0) {
- print("0 ");
- }
- else {
- print("_ ");
- }
- }
- print("\n");
- }
- }
- def checkBoard(): Int = {
- for(i <- 0 until 3) {
- if(board(i)(0) != -1) {
- if(board(i)(0) == board(i)(1) && board(i)(1) == board(i)(2)) {
- println("There is a winner! (line)");
- return 1;
- }
- }
- }
- for(i <- 0 until 3) {
- if(board(0)(i) != -1) {
- if(board(0)(i) == board(1)(i) && board(1)(i) == board(2)(i)) {
- println("There is a winner! (column)");
- return 1;
- }
- }
- }
- if(board(0)(0) == board(1)(1) && board(1)(1) == board(2)(2) && board(0)(0) != -1) {
- println("There is a winner! (Diagonal 1)");
- return 1;
- }
- if(board(0)(2) == board(1)(1) && board(1)(1) == board(2)(0) && board(0)(2) != -1) {
- println("There is a winner! (Diagonal 2)");
- return 1;
- }
- return 0;
- }
- }
- val myTicTacToe = new TicTacToe
- var player = 0;
- var play = 0;
- var c = 0;
- var l = 0;
- var ttt = 0;
- for(j <- 0 until 9 if ttt != 1) {
- printf("Time to player %d choose!\n", player + 1);
- do {
- print("Play (1) or see the game (0) ? ");
- play = io.StdIn.readInt();
- if(play == 0) {
- //show game
- myTicTacToe.seeGame();
- }
- }while(play == 0);
- do {
- print("Write the column number: ");
- c = io.StdIn.readInt();
- print("Write the line number: ");
- l = io.StdIn.readInt();
- ttt = myTicTacToe.played(c-1, l-1, player);
- }while(ttt == 2);
- player = player ^ 1;
- }
- println("End of the game!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement