Asg12 Ex1 Read text Go through all the code that has been provided (very strong
ID: 3708812 • Letter: A
Question
Asg12 Ex1
Read text
Go through all the code that has been provided
(very strong suggestion – Key in the entire program yourself. It won’t do you nearly as much good if you just copy it.)
NEED ALGORITHM create your plan based on the instructions below
Create the code
TEST
Need to have nice code
Use Comments
Submit code (does not have to be in a zipped file as there is only one program to submit)
Being able to read and modify another person’s code is essential to being a successful
computer scientist. Modify tictactoe.rb and board.rb to play tic-tac-toe on boards
of size 3 × 3, 6 × 6, and 9 × 9 based on the user’s choice. (in other words provide a menu with 4 options, 3x3 , 6x6, 9x9 and exit. Adjust everything so that
the game works as well on 6 × 6 and 9 × 9 boards as it does on 3 × 3 boards.
Note: there are a number of completed TicTacToe programs on the Internet - you may look at them. If you copy them that is wrong and will result in a zero
You can learn from them – and I would expect you to.
Use the code in the text as your basis and merely learn from the other examples.
----------
The main program (tictactoe.rb) will use the board class. The start of the main program is given in Example 12-2.
Example 12-2. Beginning of tictactoe.rb
1 require_relative 'board.rb'
2
3 puts "Starting tic-tac-toe..."
4 players = ['X', 'O']
5 current_player = players[rand(2)]
6 b = Board.new(current_player)
7 b.display()
8 puts
§ Lines 4 and 5 randomly pick an initial player (either X or O).
§ Line 6 creates an instance of the Board class and assigns it to b.
§ Lines 7 and 8 display the initial blank board and output a fresh blank line for readability.
The display method (which is part of board.rb) is given in Example 12-3.
Example 12-3. Display method for Board class
1 def display
2 puts "+- - - - - -+"
3 for row in 0..BOARD_MAX_INDEX
4 # print has to be used when we don't want to output a line break
5 print "| "
6 for col in 0..BOARD_MAX_INDEX
7 s = @board[row][col]
8 if s == EMPTY_POS
9 print col + (row * 3) + 1
10 else
11 print s
12 end
13 print " | "
14 end
15 puts " +- - - - - -+"
16 end
17 end
Example 12-4. Continuation of tictactoe.rb
1 while not b.board_full() and not b.winner()
2 b.ask_player_for_move( current_player)
3 current_player = b.get_next_turn()
4 b.display() 5 puts
6 end
7
8 if b.winner()
9 puts "Player " + b.get_next_turn() + " wins."
10 else
11 puts "Tie Game."
12 end 13 puts "Game Over"
Explanation / Answer
WINNING_LINES = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] +
[[1, 4, 7], [2, 5, 8], [3, 6, 9]] +
[[1, 5, 9], [3, 5, 7]]
INITIAL_MARKER = ' '
PLAYER_MARKER = 'X'
COMPUTER_MARKER = 'O'
def prompt(msg)
puts "=> #{msg}"
end
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def display_board(brd)
system 'clear'
puts "You're a #{PLAYER_MARKER}. Computer is a #{COMPUTER_MARKER}."
puts ""
puts " | |"
puts " #{brd[1]} | #{brd[2]} | #{brd[3]}"
puts " | |"
puts "-----+-----+-----"
puts " | |"
puts " #{brd[4]} | #{brd[5]} | #{brd[6]}"
puts " | |"
puts "-----+-----+-----"
puts " | |"
puts " #{brd[7]} | #{brd[8]} | #{brd[9]}"
puts " | |"
puts ""
end
# rubocop:enable Metrics/MethodLength, Metrics/ABcSize
def initialize_board
new_board = {}
(1..9).each { |num| new_board[num] = INITIAL_MARKER }
new_board
end
def empty_squares(brd)
brd.keys.select { |num| brd[num] == INITIAL_MARKER }
end
def players_places_piece_!(brd)
square = ''
#prompt "Choose a position to place a piece (#{empty_squares(brd).join(', ')}) "
loop do
prompt "Choose a position to place a piece (#{empty_squares(brd).join(', ')}) "
square = gets.chomp.to_i
break if empty_squares(brd).include?(square)
prompt "Sorry, that's not a valid choice."
end
brd[square] = PLAYER_MARKER
end
def computer_paces_piece!(brd)
square = empty_squares(brd).sample
brd[square] = COMPUTER_MARKER
end
def board_full?(brd)
empty_squares(brd).empty?
end
def someone_won?(brd)
!!detect_winner(brd)
end
def detect_winner(brd)
WINNING_LINES.each do |line|
if brd.values_at(*line).count(PLAYER_MARKER) == 3
return 'Player'
elsif brd.values_at(*line).count(COMPUTER_MARKER) == 3
return 'Computer'
end
end
nil
end
loop do
board = initialize_board
loop do
display_board(board)
players_places_piece_!(board)
break if someone_won?(board) || board_full?(board)
computer_paces_piece!(board)
break if someone_won?(board) || board_full?(board)
end
display_board(board)
if someone_won?(board)
prompt "#{detect_winner(board)} won! "
else
prompt "It's a tie!"
end
prompt "Play again? (y or n)"
answer = gets.chomp
break unless answer.downcase.start_with?('y')
end
prompt "Thanks for playing Tic Tac Toe! Good Bye"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.