Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. function mostrar_tablero
  5. {
  6. echo "-------------"
  7. echo "| ${tablero[0]} | ${tablero[1]} | ${tablero[2]} |"
  8. echo "-------------"
  9. echo "| ${tablero[3]} | ${tablero[4]} | ${tablero[5]} |"
  10. echo "-------------"
  11. echo "| ${tablero[6]} | ${tablero[7]} | ${tablero[8]} |"
  12. echo "-------------"
  13. }
  14.  
  15.  
  16. function destino_jugador
  17. {
  18. movimiento=$((movimiento+1))
  19. destino=-1
  20. until test "$destino" -gt 0 -a "$destino" -lt 10 -a "${tablero[$((destino-1))]}" = "-"
  21. do
  22. echo "Indique la posicion a la que quiere mover "
  23. read destino
  24. done
  25. tablero[$((destino-1))]=X
  26.  
  27. }
  28.  
  29.  
  30. function origen_jugador
  31. {
  32. origen=-1
  33.  
  34. until test "$origen" -gt 0 -a "$origen" -lt 10 -a "${tablero[$((origen-1))]}" = "X"
  35. do
  36. echo "Indique la ficha que quiere mover"
  37. read origen
  38. if test $central -eq 1 -a $origen -eq 5
  39. then
  40. echo "La ficha central no se puede mover"
  41. origen=-1
  42. fi
  43. done
  44. tablero[$((origen-1))]=-
  45.  
  46. }
  47.  
  48.  
  49. function mov_maquina
  50. {
  51. movimiento=$((movimiento+1))
  52. origen=-1
  53. if test $movimiento -gt 6
  54. then
  55. until test "$origen" -gt 0 -a "$origen" -lt 10 -a "${tablero[$((origen-1))]}" = "O"
  56. do
  57. origen=$((1 + RANDOM % 9))
  58. if test $central -eq 1 -a $origen -eq 5
  59. then
  60. origen=-1
  61. fi
  62. done
  63. tablero[$((origen-1))]=-
  64. fi
  65. destino=-1
  66. until test "$destino" -gt 0 -a "$destino" -lt 10 -a "${tablero[$((destino-1))]}" = "-" -a "$destino" -ne "$origen"
  67. do
  68. destino=$((1 + RANDOM % 9))
  69. done
  70. tablero[$((destino-1))]=O
  71.  
  72. }
  73.  
  74.  
  75. function comprobar
  76. {
  77. if test "$final" -eq 0
  78. then
  79. if test "${tablero[0]}" != "-" -a ${tablero[0]} = ${tablero[1]} -a ${tablero[1]} = ${tablero[2]}
  80. then
  81. final=1 #hay ganador
  82. ganador=${tablero[0]}
  83. fi
  84. if test ${tablero[3]} != "-" -a ${tablero[3]} = ${tablero[4]} -a ${tablero[4]} = ${tablero[5]}
  85. then
  86. final=1 #hay ganador
  87. ganador=${tablero[3]}
  88. fi
  89. if test ${tablero[6]} != "-" -a ${tablero[6]} = ${tablero[7]} -a ${tablero[7]} = ${tablero[8]}
  90. then
  91. final=1 #hay ganador
  92. ganador=${tablero[6]}
  93. fi
  94. if test ${tablero[0]} != "-" -a ${tablero[0]} = ${tablero[3]} -a ${tablero[3]} = ${tablero[6]}
  95. then
  96. final=1 #hay ganador
  97. ganador=${tablero[0]}
  98. fi
  99. if test ${tablero[1]} != "-" -a ${tablero[1]} = ${tablero[4]} -a ${tablero[4]} = ${tablero[7]}
  100. then
  101. final=1 #hay ganador
  102. ganador=${tablero[1]}
  103. fi
  104. if test ${tablero[2]} != "-" -a ${tablero[2]} = ${tablero[5]} -a ${tablero[5]} = ${tablero[8]}
  105. then
  106. final=1 #hay ganador
  107. ganador=${tablero[2]}
  108. fi
  109. if test ${tablero[0]} != "-" -a ${tablero[0]} = ${tablero[4]} -a ${tablero[4]} = ${tablero[8]}
  110. then
  111. final=1 #hay ganador
  112. ganador=${tablero[0]}
  113. fi
  114. if test ${tablero[2]} != "-" -a ${tablero[2]} = ${tablero[4]} -a ${tablero[4]} = ${tablero[6]}
  115. then
  116. final=1 #hay ganador
  117. ganador=${tablero[2]}
  118. fi
  119. fi
  120. #Comprueba si hay ganador o no
  121. }
  122.  
  123.  
  124.  
  125.  
  126. function jugar
  127. {
  128. empieza=$comienzo
  129. ficha5=$central
  130. tablero=(- - - - - - - - -)
  131. movimiento=0
  132. final=0
  133. ficha=" "
  134. jugador=0
  135.  
  136. #Determinar quien empieza el juego
  137. if test $empieza -eq 1
  138. then
  139. jugador=1
  140. echo "Empieza a jugar el humano"
  141.  
  142. elif test $empieza -eq 2
  143. then
  144. jugador=2
  145. echo "Empieza a jugar la computadora"
  146. else
  147. echo "El primer jugador se elige aleatoriamente"
  148. empieza=$(( 1+ RANDOM % 2))
  149. if test $empieza -eq 1
  150. then
  151. jugador=1
  152. echo "Empieza a jugar el humano"
  153. else
  154. jugador=2
  155. echo "Empieza a jugar la computadora"
  156. fi
  157. fi
  158.  
  159. #Se ejecuta el juego
  160. if test $jugador -eq 1
  161. then
  162. while test $movimiento -ne 6 -a "$final" -ne 1
  163. do
  164. mostrar_tablero
  165. destino_jugador
  166. comprobar
  167. mov_maquina
  168. comprobar
  169. done
  170. until test $final -eq 1
  171. do
  172. mostrar_tablero
  173. origen_jugador
  174. destino_jugador
  175. comprobar
  176. mov_maquina
  177. comprobar
  178. done
  179.  
  180.  
  181. else
  182. while test $movimiento -ne 6 -a "$final" -ne 1
  183. do
  184. mov_maquina
  185. comprobar
  186. mostrar_tablero
  187. destino_jugador
  188. comprobar
  189. done
  190. until test $final -eq 1
  191. do
  192. mov_maquina
  193. comprobar
  194. if test "$final" -eq 1
  195. then
  196. break
  197. fi
  198. mostrar_tablero
  199. origen_jugador
  200. destino_jugador
  201. comprobar
  202. done
  203. fi
  204.  
  205. echo "El ganador ha sido el jugador $ganador, pulse cualquier tecla para continuar >>"
  206. read -n 1
  207.  
  208.  
  209.  
  210. }
  211.  
  212.  
  213. function leerconfiguracion
  214. {
  215. comienzo=-1
  216. central=-1
  217. ruta="/home/alumno/Desktop/trabajo/oxo.log"
  218. while read linea
  219. do
  220. if test "${linea:0:8}" = "COMIENZO"
  221. then
  222. comienzo="${linea:9}"
  223. fi
  224. if test "${linea:0:12}" = "FICHACENTRAL"
  225. then
  226. central="${linea:13}"
  227. fi
  228. if test "${linea:0:12}" = "ESTADISTICAS"
  229. then
  230. ruta="${linea:13}"
  231. fi
  232. done < oxo.cfg
  233.  
  234. }
  235.  
  236.  
  237.  
  238. if test "$1" = "-g"
  239. then
  240. echo "Juego creado por la seƱora subnormal y el carritos"
  241. else
  242. leerconfiguracion
  243. read -p "Pulse INTRO para continuar"
  244. jugar
  245.  
  246. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement