Class: GamesAndRpgParadise::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/engine.rb

Overview

GamesAndRpgParadise::Engine

Constant Summary collapse

POSITION =
#

POSITION

#
%w(
  1 2 3 4 5 6 7 8 9
)
INFO =
#

INFO

#
<<EOF

************************************************************************
                Tic-Tac-Toe game in Ruby
************************************************************************

EOF

Instance Method Summary collapse

Constructor Details

#initializeEngine

#

initialize

#


42
43
44
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/engine.rb', line 42

def initialize
  run
end

Instance Method Details

#check_winner(board) ⇒ Object

#

check_winner

#


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/engine.rb', line 125

def check_winner(board)
  x_count = 0
  o_count = 0
  Board::WINNING_PLACES.each { |winning_place|
    winning_place.each { |index|
      if board.positions_with_values["#{index}"] == "X"
        x_count = x_count + 1
      elsif board.positions_with_values["#{index}"] == "O"
        o_count = o_count + 1
      end
    }
    if x_count == 3 or o_count == 3
      break
    else
      x_count = 0
      o_count = 0
    end
  }
  if x_count == 3
    return 'X won'
  elsif o_count == 3
    return 'O won'
  end
  return 'No One'
end

#display_infoObject

#

display_info

#


56
57
58
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/engine.rb', line 56

def display_info
  e INFO
end

#display_winner(i) ⇒ Object

#

display_winner

#


154
155
156
157
158
159
160
161
162
163
164
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/engine.rb', line 154

def display_winner(i)
  e "\n*************| Result |*************"
  case i
  when 'X'
    e 'Congratulation - you won the game.'
  else
    e 'Sorry, you lost the game.'
  end
  e
  exit
end

#play(current_player, board) ⇒ Object

#

play

#


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/engine.rb', line 97

def play(current_player, board)
  case current_player.mark
  when 'X'
    flag = true
    while flag do
      e
      print "Where do want to move? <1-9>: "
      position = gets.chomp

      if !POSITION.include?(position)
        e "\nInvalid input, Please choose number between 1 to 9\n"
        flag = true
      elsif %w[X O].include?board.positions_with_values[position]
        e "\nPosition already occupied, Please choose another number...\n"
        flag = true
      else
        flag = false
      end
    end
    current_player.move(board, position, self)
  else
    current_player.best_move(board, self)
  end
end

#runObject

#

run

#


49
50
51
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/engine.rb', line 49

def run
  display_info
end

#start(x_player, o_player, board) ⇒ Object

#

start

#


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/engine.rb', line 63

def start(x_player, o_player, board)

  e "\n >> PLEASE SEE THE POSITIONS OF THE BOARD << \n\n"

  print "Do you want to play first? <y/n>: "
  ans = gets.chomp

  if %w[N n].include?(ans)
    current_player = o_player
  else
    current_player = x_player
  end

  (1..9).each {
    if current_player == x_player
      play(current_player, board)
      current_player = o_player
    else
      play(current_player, board)
      current_player = x_player
    end
  }
end

#stopObject

#

stop

#


90
91
92
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/engine.rb', line 90

def stop
  e "\n************* Match Draw ****************\n\n"
end