Home
Preface
Chapter Selection
Programming Examples
Homework Problems
Homework Solutions MEA's
Search Useful Links

Programming Examples

Chapter 8 Programming Examples

Board Object: Stores the Tic-Tac-Toe board
1  class Board
2      EMPTY_POS =' '
3      def initialize
4         @current_player = 'x'
5         size = 3
6         @board  = Array.new(size){Array.new(size) { EMPTY_POS
7      end


 
 
Tic-Tac-Toe Program
1  require 'board.rb'
2  puts "Starting tic-tac-toe..."
3  b = Board.new()
4  b.display()
5  current_player = 'x'
6  while not b.board_full() and not b.winner()
7        b.ask_player_for_move(current_player)
8        current_player = b.get_next_turn()
9        b.display()
10 end
11 if b.winner()
12    puts "Player " +  b.get_next_turn() + "wins." 
13 else
14    puts "Tie Game."
15 end
16    puts "Game Over"


 
 
Displaying a Tic-Tac-Toe Board
1  # Print the board array and for all blank positions, just output the number 1-9
2	
3  def display
4        puts '+- - - - - - - - - - -+'
5        for row in 0..2
6             # print has to be used when we don't  want to output a line break
7             print '| '
8             for col in 0..2
9  	         s = @board[row][col]
10  	         if s == EMPTY_POS
11	             print col + (row * 3) + 1
12	         else
13	             print s
14       	 end
15	         print ' | '
16            end
17            puts
18            puts '+- - - - - - - - - - -+'
19       end
20 end


 
 
board_full()method-->Check for any open positions
1  # Scan the board and if we see any open positions we are done
2  def board_full
3       @board.each do |row|
4            row.each do |position|
5          if position == EMPTY_POS
6            return false
7          end
8       end
9       end
10      # Scanned entire board, so there are no more open positions
11      return true
12 end


 
 
winner() method: Checking any Winning Combinations
1  def winner
2      winner = winner_rows
3      if winner
4         return winner
5      end
6      winner = winner_cols
7      if winner
8         return winner
9      end
10     winner = winner_diagonals
11     if winner
12        return winner
13     end
14     # No winners
15    return
16 end


 
 
winner_rows() method: Checking Rows for Three in a Row
1  def winner_rows
2    for row_index in 0..board_max_index
3        first_symbol = @board[row_index][0]
4        for col_index in 1..board_max_index
5           if	first_symbol !=  @board[row_index][col_index]
6	      break
7           elsif col_index == board_max_index and first_symbol != EMPTY_POS
8	      return first_symbol
9           end
10       end
11   end
12  return
13 end


 
 
winner_cols() method: Checking Columns for Three in a Row
1  def winner_cols
2     for col_index in 0..board_max_index
3           first_symbol = @board[0][col_index]
4         for row_index in 1..board_max_index
5             if first_symbol != @board[row_index][col_index]
6	         break
7             elsif row_index== board_max_indexand first_symbol !=EMPTY_POS
8                return first_symbol
9  	      end
10        end
11    end
12   return
13 end


 
 
winner_diagonals() method: Checking Both Diagonals for Three in a Row
1  def winner_diagonals
2     first_symbol = @board[0][0]
3     for index in 1..board_max_index
4         if first_symbol!=@board[index][index]
5            break
6         elsif index == board_max_index and first_symbol != EMPTY_POS
7            return first_symbol
8         end
9     end
10    first_symbol = @board[0][board_max_index]
11    row_index = 0
12    col_index = board_max_index
13    while row_index < board_max_index
14        row_index = row_index + 1
15        col_index = col_index – 1
16        if first_symbol !=@board[row_index][col_index]
17           break
18        elsif row_index == board_max_index and first_symbol != EMPTY_POS
19           return first_symbol
20        end
21    end
22  return
23 end


 
 
ask_player_for_move() method:Asking User where they wish to move
1  def ask_player_for_move(current_player)
2    played = false
3    while not played
4         puts "Player " + current_player + ": Where would you like to play?"
5         move = gets.to_i – 1
6         col = move % @board.size
7         row = (move - col) / @board.size
8         if validate_position(row, col)
9           @board[row][col] = @current_player
10          played = true
11        end
12   end
13 end


 
 
validate_position() method:Checks to see if position chosen is allowed
1  def validate_position(row, col)
2    if row <= @board.size and col <= @board.size
3      if @board[row][col] == EMPTY_POS
4         return true
5      else
6         puts "That position is occupied."
7      end
8    else
9      puts "Invalid position."
10   end
11 end


 
 
get_next_turn() method:Changes Players
1  def get_next_turn
2    if @current_player == 'x'
3       @current_player = 'o'
4    else
5       @current_player = 'x'
6    end
7    return @current_player
8 end