Thursday, October 19, 2006

Primer juego en Ruby!!!


Ayer en la noche, luego de unos pequeños ajustes...Terminé mi primer juego en Ruby...El famoso juego Michi, Gato, Tic-Tac-Toe o como quieran llamarlo -;)
Como está hecho en Ruby tiene una simple interface de DOS, aunque es más que suficiente para un juego sencillo -:)



Este es el código fuente por si quieren revisarlo -:)


#TIC_TAC_TOE

$game = false
$cheat = false
$player_won = ""
$player_turn = "1"

def clean_lines()
$line1 = ""
$line2 = ""
$line3 = ""
$line4 = ""
$line5 = ""
end

def initial_board()
clean_lines()
$line1 << " | | "
$line2 << "---+---+---"
$line3 << " | | "
$line4 << "---+---+---"
$line5 << " | | "

print "#{$line1}\n"
print "#{$line2}\n"
print "#{$line3}\n"
print "#{$line4}\n"
print "#{$line5}\n\n"
end

def board(coordenates,player)
clean_lines()

if $board_array[coordenates] == nil
$board_array[coordenates] = player
else
cheat = true
end

$Move_1 = $board_array[0]
if $Move_1 == nil
$Move_1 = " "
end
$Move_2 = $board_array[1]
if $Move_2 == nil
$Move_2 = " "
end
$Move_3 = $board_array[2]
if $Move_3 == nil
$Move_3 = " "
end
$Move_4 = $board_array[3]
if $Move_4 == nil
$Move_4 = " "
end
$Move_5 = $board_array[4]
if $Move_5 == nil
$Move_5 = " "
end
$Move_6 = $board_array[5]
if $Move_6 == nil
$Move_6 = " "
end
$Move_7 = $board_array[6]
if $Move_7 == nil
$Move_7 = " "
end
$Move_8 = $board_array[7]
if $Move_8 == nil
$Move_8 = " "
end
$Move_9 = $board_array[8]
if $Move_9 == nil
$Move_9 = " "
end

$line1 << " #{$Move_1} | #{$Move_2} | #{$Move_3} "
$line2 << "---+---+---"
$line3 << " #{$Move_4} | #{$Move_5} | #{$Move_6} "
$line4 << "---+---+---"
$line5 << " #{$Move_7} | #{$Move_8} | #{$Move_9} "

print "#{$line1}\n"
print "#{$line2}\n"
print "#{$line3}\n"
print "#{$line4}\n"
print "#{$line5}\n\n"
end

def check_who_wins()
if $board_array[0] != nil &&
$board_array[0] == $board_array[1] &&
$board_array[1] == $board_array[2]
$game = true
if $board_array[0] == "*"
print "El Jugador 1 gano\n\n"
else
print "El Jugador 2 gano\n\n"
end
end
if $board_array[3] != nil &&
$board_array[3] == $board_array[4] &&
$board_array[4] == $board_array[5]
$game = true
if $board_array[3] == "*"
print "El Jugador 1 gano\n\n"
else
print "El Jugador 2 gano\n\n"
end
end
if $board_array[0] != nil &&
$board_array[0] == $board_array[3] &&
$board_array[3] == $board_array[6]
$game = true
if $board_array[0] == "*"
print "El Jugador 1 gano\n\n"
else
print "El Jugador 2 gano\n\n"
end
end
if $board_array[1] != nil &&
$board_array[1] == $board_array[4] &&
$board_array[4] == $board_array[7]
$game = true
if $board_array[1] == "*"
print "El Jugador 1 gano\n\n"
else
print "El Jugador 2 gano\n\n"
end
end
if $board_array[2] != nil &&
$board_array[2] == $board_array[5] &&
$board_array[5] == $board_array[8]
$game = true
if $board_array[2] == "*"
print "El Jugador 1 gano\n\n"
else
print "El Jugador 2 gano\n\n"
end
end
if $board_array[0] != nil &&
$board_array[0] == $board_array[4] &&
$board_array[4] == $board_array[8]
$game = true
if $board_array[0] == "*"
print "El Jugador 1 gano\n\n"
else
print "El Jugador 2 gano\n\n"
end
end
if $board_array[2] != nil &&
$board_array[2] == $board_array[4] &&
$board_array[4] == $board_array[6]
$game = true
if $board_array[2] == "*"
print "El Jugador 1 gano\n\n"
else
print "El Jugador 2 gano\n\n"
end
end
end

$board_array = Array.new
$played_moves = Array.new
initial_board()

while($game == false)

if $player_turn == "1"
print "Jugador 1: "
$coordenates = gets
$coordenates = $coordenates.chop!.to_i
if $coordenates <= 0 || $coordenates > 9
print "Por favor ingresar valores del 1 al 9\n"
redo
end
for moves in $played_moves
moves += 1
if moves == $coordenates
$cheat = true
end
end
if $cheat == true
$cheat = false
print "Esa jugada ya ha sido hecha!\n"
redo
end
$coordenates -= 1
$played_moves.push($coordenates)
$player = "*"
board($coordenates,$player)
check_who_wins()
$player_turn = "2"
end

if $game == true
break
end

if $player_turn == "2"
print "Jugador 2: "
$coordenates = gets
$coordenates = $coordenates.chop!.to_i
if $coordenates <= 0 || $coordenates > 9
print "Por favor ingresar valores del 1 al 9\n"
redo
end
for moves in $played_moves
moves += 1
if moves == $coordenates
$cheat = true
end
end
if $cheat == true
$cheat = false
print "Esa jugada ya ha sido hecha!\n"
redo
end
$coordenates -= 1
$played_moves.push($coordenates)
$player = "O"
board($coordenates,$player)
check_who_wins()
$player_turn = "1"
end

if $game == true
break
end

print "#{$player_won}"
end

Saludos,

Blag.

2 comments:

Anonymous said...

Y dónde quedó la POO?

Alvaro "Blag" Tejada Galindo said...

Pues dentro de Ruby...En Ruby...Todos son objetos -;)

Saludos,

Blag.