Here is an interaction in a tictactoe game with user input i

Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new(\'andy\', \'mike\') => # >> gm.play_game(\'mike\') Mike, enter your next O move (1-9): 1 O-- --- --- Andy, enter your next X move (1-9): 3 O-X --- --- Mike, enter your next O move (1-9): 5 O-X -O- --- Andy, enter your next X move (1-9): 3 Bad move dude! You go again. O-X -O- --- Andy, enter your next X move (1-9): 6 O-X - OX --- Mike, enter your next O move (1-9): 9 O-X - OX --O Mike, you won! => nil >> gm.play_game(\'karen\') I don\'t know that player. Please try again. => nil >> gm.play_game(\'andy\') Andy, enter your next O move (1-9): 5 --- -O- --- Implement this output using Ruby

Solution

Answer:

class BoardTic

def initializeBoard

    @Board1 = Array.new(3) { Array.new(3, \" \") }

end

def printInstructions

    puts \"1 | 2 | 3\",

         \"---------\",

         \"4 | 5 | 6\",

         \"---------\",

         \"7 | 8 | 9\"

    print \"\ \"

end

def printBoard1

    (0..2).each do |rows|

      print \"       \"

      (0..2).each do |cols|

        print @Board1[rows][cols]

        print \" | \" unless cols == 2

      end

      print \"\ \"

      print \"       ---------\ \" unless rows == 2

    end

    print \"\ \"

end

def isTie

    @Board1.join.split(\'\').include?(\" \")

end

def findWinnerTac

    (0..2).each do |i|

      if @Board1[i][0] == @Board1[i][1] && @Board1[i][1] == @Board1[i][2]

        return @Board1[i][0] unless @Board1[i][0] == \" \"

      elsif @Board1[0][i] == @Board1[1][i] && @Board1[1][i] == @Board1[2][i]

        return @Board1[0][i] unless @Board1[0][i] == \" \"

      end

    end

    if ( @Board1[0][0] == @Board1[1][1] && @Board1[1][1] == @Board1[2][2] ) ||

       ( @Board1[0][2] == @Board1[1][1] && @Board1[1][1] == @Board1[2][0] )

      return @Board1[1][1] unless @Board1[1][1] == \" \"

    end

   

    return \"C\" unless isTie

    return false

end

def is_empty(rows,cols)

      @Board1[rows][cols] === \" \"

end

def inside_Board1(rows,cols)

      (0..2) === rows and (0..2) === cols

end

def valid_moves(rows,cols)

      is_empty(rows,cols) and inside_Board1(rows,cols)

end

def dropPiece(piece, rows, cols)

    @Board1[rows][cols] = piece if valid_moves(rows,cols)

end

end

def clear_screen

puts \"\ \" * 100

end

def prompt_moves(active_players)

puts \" #{active_players}\'s turn. Choose a box!\",

       \"        **~V~**\"

print \"           \"

moveVal= gets.chomp.to_i - 1

rows = moveVal/ 3

cols = moveVal% 3

return rows,cols

end

def alert_winner(winnerS,Board1)

puts \"   --**~^^^^^^^~**--\"

    if winnerS == \"C\"

      puts \"   C A T S   G A M E\"

    else

      puts \"     #{winnerS} \' S   W I N\"

    end

puts \"   ----**~vVv~**----\"

puts \"\ \"

Board1.printBoard1

puts \"\         **~V~**\"

end

def ticTacToe(Board1Class)

Board1 = Board1Class.new

active_players = \"X\"

clear_screen

Board1.printInstructions

while not Board1.findWinnerTac

    rows,cols = prompt_moves(active_players)

    clear_screen

    if Board1.dropPiece(active_players, rows, cols)

      if active_players == \"X\"

        active_players = \"O\"

      else

        active_players = \"X\"

      end

    else

      puts \"                 INVALID MOVE, PLEASE SELECT AGAIN\ \ \"

    end

    Board1.printBoard1

end

winnerS = Board1.findWinnerTac

clear_screen

alert_winner(winnerS,Board1)

end

while true

clear_screen

puts \"DO YOU WANT TO PLAY AGAIN? (y/n)\"

if [\"no\",\"n\"].include? (gets.chomp.downcase)

    puts \"GOODBYE\"

    break

end

ticTacToe(Board1)

end

Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new(\'andy\', \'mike\') => # >> gm.play_game(\'mike\') Mike,
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new(\'andy\', \'mike\') => # >> gm.play_game(\'mike\') Mike,
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new(\'andy\', \'mike\') => # >> gm.play_game(\'mike\') Mike,
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new(\'andy\', \'mike\') => # >> gm.play_game(\'mike\') Mike,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site