Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function mostrar_tablero
- {
- echo "-------------"
- echo "| ${tablero[0]} | ${tablero[1]} | ${tablero[2]} |"
- echo "-------------"
- echo "| ${tablero[3]} | ${tablero[4]} | ${tablero[5]} |"
- echo "-------------"
- echo "| ${tablero[6]} | ${tablero[7]} | ${tablero[8]} |"
- echo "-------------"
- }
- function destino_jugador
- {
- movimiento=$((movimiento+1))
- destino=-1
- until test "$destino" -gt 0 -a "$destino" -lt 10 -a "${tablero[$((destino-1))]}" = "-"
- do
- echo "Indique la posicion a la que quiere mover "
- read destino
- done
- tablero[$((destino-1))]=X
- }
- function origen_jugador
- {
- origen=-1
- until test "$origen" -gt 0 -a "$origen" -lt 10 -a "${tablero[$((origen-1))]}" = "X"
- do
- echo "Indique la ficha que quiere mover"
- read origen
- if test $central -eq 1 -a $origen -eq 5
- then
- echo "La ficha central no se puede mover"
- origen=-1
- fi
- done
- tablero[$((origen-1))]=-
- }
- function mov_maquina
- {
- movimiento=$((movimiento+1))
- origen=-1
- if test $movimiento -gt 6
- then
- until test "$origen" -gt 0 -a "$origen" -lt 10 -a "${tablero[$((origen-1))]}" = "O"
- do
- origen=$((1 + RANDOM % 9))
- if test $central -eq 1 -a $origen -eq 5
- then
- origen=-1
- fi
- done
- tablero[$((origen-1))]=-
- fi
- destino=-1
- until test "$destino" -gt 0 -a "$destino" -lt 10 -a "${tablero[$((destino-1))]}" = "-" -a "$destino" -ne "$origen"
- do
- destino=$((1 + RANDOM % 9))
- done
- tablero[$((destino-1))]=O
- }
- function comprobar
- {
- if test "$final" -eq 0
- then
- if test "${tablero[0]}" != "-" -a ${tablero[0]} = ${tablero[1]} -a ${tablero[1]} = ${tablero[2]}
- then
- final=1 #hay ganador
- ganador=${tablero[0]}
- fi
- if test ${tablero[3]} != "-" -a ${tablero[3]} = ${tablero[4]} -a ${tablero[4]} = ${tablero[5]}
- then
- final=1 #hay ganador
- ganador=${tablero[3]}
- fi
- if test ${tablero[6]} != "-" -a ${tablero[6]} = ${tablero[7]} -a ${tablero[7]} = ${tablero[8]}
- then
- final=1 #hay ganador
- ganador=${tablero[6]}
- fi
- if test ${tablero[0]} != "-" -a ${tablero[0]} = ${tablero[3]} -a ${tablero[3]} = ${tablero[6]}
- then
- final=1 #hay ganador
- ganador=${tablero[0]}
- fi
- if test ${tablero[1]} != "-" -a ${tablero[1]} = ${tablero[4]} -a ${tablero[4]} = ${tablero[7]}
- then
- final=1 #hay ganador
- ganador=${tablero[1]}
- fi
- if test ${tablero[2]} != "-" -a ${tablero[2]} = ${tablero[5]} -a ${tablero[5]} = ${tablero[8]}
- then
- final=1 #hay ganador
- ganador=${tablero[2]}
- fi
- if test ${tablero[0]} != "-" -a ${tablero[0]} = ${tablero[4]} -a ${tablero[4]} = ${tablero[8]}
- then
- final=1 #hay ganador
- ganador=${tablero[0]}
- fi
- if test ${tablero[2]} != "-" -a ${tablero[2]} = ${tablero[4]} -a ${tablero[4]} = ${tablero[6]}
- then
- final=1 #hay ganador
- ganador=${tablero[2]}
- fi
- fi
- #Comprueba si hay ganador o no
- }
- function jugar
- {
- empieza=$comienzo
- ficha5=$central
- tablero=(- - - - - - - - -)
- movimiento=0
- final=0
- ficha=" "
- jugador=0
- #Determinar quien empieza el juego
- if test $empieza -eq 1
- then
- jugador=1
- echo "Empieza a jugar el humano"
- elif test $empieza -eq 2
- then
- jugador=2
- echo "Empieza a jugar la computadora"
- else
- echo "El primer jugador se elige aleatoriamente"
- empieza=$(( 1+ RANDOM % 2))
- if test $empieza -eq 1
- then
- jugador=1
- echo "Empieza a jugar el humano"
- else
- jugador=2
- echo "Empieza a jugar la computadora"
- fi
- fi
- #Se ejecuta el juego
- if test $jugador -eq 1
- then
- while test $movimiento -ne 6 -a "$final" -ne 1
- do
- mostrar_tablero
- destino_jugador
- comprobar
- mov_maquina
- comprobar
- done
- until test $final -eq 1
- do
- mostrar_tablero
- origen_jugador
- destino_jugador
- comprobar
- mov_maquina
- comprobar
- done
- else
- while test $movimiento -ne 6 -a "$final" -ne 1
- do
- mov_maquina
- comprobar
- mostrar_tablero
- destino_jugador
- comprobar
- done
- until test $final -eq 1
- do
- mov_maquina
- comprobar
- if test "$final" -eq 1
- then
- break
- fi
- mostrar_tablero
- origen_jugador
- destino_jugador
- comprobar
- done
- fi
- echo "El ganador ha sido el jugador $ganador, pulse cualquier tecla para continuar >>"
- read -n 1
- }
- function leerconfiguracion
- {
- comienzo=-1
- central=-1
- ruta="/home/alumno/Desktop/trabajo/oxo.log"
- while read linea
- do
- if test "${linea:0:8}" = "COMIENZO"
- then
- comienzo="${linea:9}"
- fi
- if test "${linea:0:12}" = "FICHACENTRAL"
- then
- central="${linea:13}"
- fi
- if test "${linea:0:12}" = "ESTADISTICAS"
- then
- ruta="${linea:13}"
- fi
- done < oxo.cfg
- }
- if test "$1" = "-g"
- then
- echo "Juego creado por la seƱora subnormal y el carritos"
- else
- leerconfiguracion
- read -p "Pulse INTRO para continuar"
- jugar
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement