Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. package jogodavelha;
  2.  
  3. import java.util.*;
  4.  
  5. /* @author Vitor Rebello */
  6.  
  7. public class JogoDaVelha {
  8. //Membros do jogador:
  9. static int jogadorAtual, casasVazias;
  10. static char[][] tabuleiro;
  11. static boolean fimDeJogo;
  12. static String jogada;
  13.  
  14.  
  15. /**
  16. * @param args the command line arguments
  17. */
  18. public static void main(String[] args) {
  19. //Inicializando o Tabueliro:
  20. Scanner ler = new Scanner (System.in);
  21. tabuleiro = new char[3][3];
  22.  
  23. for(int x = 0; x < 3; x++) {
  24. for(int y = 0; y < 3; y++) {
  25. tabuleiro[x][y] = ' ';
  26. }
  27. }
  28.  
  29. casasVazias = 9;
  30.  
  31. //Definindo o jogador aleatório:
  32. Random tmp = new Random();
  33. jogadorAtual = tmp.nextInt(2) + 1;
  34.  
  35. //Loop Principal:
  36. do
  37. {
  38. //Buscando a entrada do jogador atual:
  39. boolean jogadaValida;
  40.  
  41. do
  42. {
  43. //Imprimindo o tabuleiro:
  44. Imprimir_Jogo();
  45.  
  46. //Pedindo a entrada do Jogador:
  47. System.out.print("Jogador " + jogadorAtual + ", entre com a posicao que deseja jogar (1 ~ 9): ");
  48. jogada = ler.next();
  49.  
  50. jogadaValida = Valida_Jogada(jogada);
  51. }while(!jogadaValida);
  52.  
  53. //Trocando o jogador atual:
  54. if(jogadorAtual == 1) {
  55. jogadorAtual = 2;
  56. }else {
  57. jogadorAtual = 1;
  58. }
  59.  
  60. //Verificando se Alguém ganhou:
  61. int resultadoPartida = verificaStatus(tabuleiro);
  62.  
  63. if(resultadoPartida != 0) {
  64. fimDeJogo = true;
  65. exibeVitoria(resultadoPartida);
  66. }
  67. else {
  68. //Preenchemos todas as casas?
  69. if(casasVazias == 0) {
  70. fimDeJogo = true;
  71. exibeVitoria(resultadoPartida);
  72. }
  73. }
  74. }while(!fimDeJogo);
  75. }
  76.  
  77. //Função que exibe o ganhador:
  78. public static void exibeVitoria(int vencedor) {
  79. //Limpando a tela:
  80.  
  81. if(vencedor == 1 || vencedor == 2) {
  82. Imprimir_Jogo();
  83. System.out.println("|-----------------------------------------------------------------|");
  84. System.out.println("| PARABÉNS! |");
  85. System.out.println("|-----------------------------------------------------------------|");
  86. System.out.println();
  87. System.out.println("JOGADOR " + vencedor + " VENCEU!");
  88. }
  89. else {
  90. Imprimir_Jogo();
  91. System.out.println("|-----------------------------------------------------------------|");
  92. System.out.println("| FIM DE JOGO! |");
  93. System.out.println("|-----------------------------------------------------------------|");
  94. System.out.println();
  95. System.out.println();
  96. System.out.println("NINGUEM GANHOU... JOGUE NOVAMENTE!");
  97. }
  98. }
  99.  
  100. //Função que verifica se o jogo acabou:
  101. public static int verificaStatus(char[][] tabuleiroAtual) {
  102. //Verificando a primeira linha:
  103. if(tabuleiroAtual[0][0] == tabuleiroAtual[0][1] && tabuleiroAtual[0][1] == tabuleiroAtual[0][2] && tabuleiroAtual[0][0] != ' ') {
  104. if(tabuleiroAtual[0][0] == 'X')
  105. return 1;
  106. if(tabuleiroAtual[0][0] == 'O')
  107. return 2;
  108. }
  109.  
  110. //Verificando a Segunda linha:
  111. if(tabuleiroAtual[1][0] == tabuleiroAtual[1][1] && tabuleiroAtual[1][1] == tabuleiroAtual[1][2] && tabuleiroAtual[1][0] != ' ') {
  112. if(tabuleiroAtual[1][0] == 'X')
  113. return 1;
  114. if(tabuleiroAtual[1][0] == 'O')
  115. return 2;
  116. }
  117.  
  118. //Verificando a Terceira linha:
  119. if(tabuleiroAtual[2][0] == tabuleiroAtual[2][1] && tabuleiroAtual[2][1] == tabuleiroAtual[2][2] && tabuleiroAtual[2][0] != ' ') {
  120. if(tabuleiroAtual[2][0] == 'X')
  121. return 1;
  122. if(tabuleiroAtual[2][0] == 'O')
  123. return 2;
  124. }
  125.  
  126. //Verificando a primeira coluna:
  127. if(tabuleiroAtual[0][0] == tabuleiroAtual[1][0] && tabuleiroAtual[1][0] == tabuleiroAtual[2][0] && tabuleiroAtual[0][0] != ' ') {
  128. if(tabuleiroAtual[0][0] == 'X')
  129. return 1;
  130. if(tabuleiroAtual[0][0] == 'O')
  131. return 2;
  132. }
  133.  
  134. //Verificando a segunda coluna:
  135. if(tabuleiroAtual[0][1] == tabuleiroAtual[1][1] && tabuleiroAtual[1][1] == tabuleiroAtual[2][1] && tabuleiroAtual[0][1] != ' ') {
  136. if(tabuleiroAtual[0][1] == 'X')
  137. return 1;
  138. if(tabuleiroAtual[0][1] == 'O')
  139. return 2;
  140. }
  141.  
  142. //Verificando a terceira coluna:
  143. if(tabuleiroAtual[0][2] == tabuleiroAtual[1][2] && tabuleiroAtual[1][2] == tabuleiroAtual[2][2] && tabuleiroAtual[0][2] != ' ') {
  144. if(tabuleiroAtual[0][2] == 'X')
  145. return 1;
  146. if(tabuleiroAtual[0][2] == 'O')
  147. return 2;
  148. }
  149.  
  150. //Verificando a Diagonal 1:
  151. if(tabuleiroAtual[0][0] == tabuleiroAtual[1][1] && tabuleiroAtual[1][1] == tabuleiroAtual[2][2] && tabuleiroAtual[0][0] != ' ') {
  152. if(tabuleiroAtual[0][0] == 'X')
  153. return 1;
  154. if(tabuleiroAtual[0][0] == 'O')
  155. return 2;
  156. }
  157.  
  158. //Verificando a Diagonal 2:
  159. if(tabuleiroAtual[0][2] == tabuleiroAtual[1][1] && tabuleiroAtual[1][1] == tabuleiroAtual[2][0] && tabuleiroAtual[0][2] != ' ') {
  160. if(tabuleiroAtual[0][2] == 'X')
  161. return 1;
  162. if(tabuleiroAtual[0][2] == 'O')
  163. return 2;
  164. }
  165.  
  166. return 0;
  167. }
  168.  
  169.  
  170. //Função que valida a jogada:
  171. public static boolean Valida_Jogada(String jogada) {
  172. //Buscando o jogador atual:
  173. char caractere;
  174.  
  175. if(jogadorAtual == 1) {
  176. caractere = 'X';
  177. }
  178. else {
  179. caractere = 'O';
  180. }
  181.  
  182. //Convertendo para int:
  183. int tmp;
  184.  
  185. try {
  186. tmp = Integer.parseInt(jogada);
  187. }catch(Exception e) {
  188. //Jogada inválida. Não é um número:
  189. return false;
  190. }
  191.  
  192. //Verificando se está entre 1 e 9:
  193. if(tmp < 1 || tmp > 9) {
  194. return false;
  195. }
  196.  
  197. //Verificando se a posição no tabuleiro pode ser jogada:
  198. switch(tmp) {
  199. case 1:
  200. if(tabuleiro[0][0] != ' ') {
  201. return false;
  202. }
  203. else {
  204. tabuleiro[0][0] = caractere;
  205. casasVazias -= 1;
  206. }
  207. break;
  208. case 2:
  209. if(tabuleiro[0][1] != ' ') {
  210. return false;
  211. }
  212. else {
  213. tabuleiro[0][1] = caractere;
  214. casasVazias -= 1;
  215. }
  216. break;
  217. case 3:
  218. if(tabuleiro[0][2] != ' ') {
  219. return false;
  220. }
  221. else {
  222. tabuleiro[0][2] = caractere;
  223. casasVazias -= 1;
  224. }
  225. break;
  226. case 4:
  227. if(tabuleiro[1][0] != ' ') {
  228. return false;
  229. }
  230. else {
  231. tabuleiro[1][0] = caractere;
  232. casasVazias -= 1;
  233. }
  234. break;
  235. case 5:
  236. if(tabuleiro[1][1] != ' ') {
  237. return false;
  238. }
  239. else {
  240. tabuleiro[1][1] = caractere;
  241. casasVazias -= 1;
  242. }
  243. break;
  244. case 6:
  245. if(tabuleiro[1][2] != ' ') {
  246. return false;
  247. }
  248. else {
  249. tabuleiro[1][2] = caractere;
  250. casasVazias -= 1;
  251. }
  252. break;
  253. case 7:
  254. if(tabuleiro[2][0] != ' ') {
  255. return false;
  256. }
  257. else {
  258. tabuleiro[2][0] = caractere;
  259. casasVazias -= 1;
  260. }
  261. break;
  262. case 8:
  263. if(tabuleiro[2][1] != ' ') {
  264. return false;
  265. }
  266. else {
  267. tabuleiro[2][1] = caractere;
  268. casasVazias -= 1;
  269. }
  270. break;
  271. case 9:
  272. if(tabuleiro[2][2] != ' ') {
  273. return false;
  274. }
  275. else {
  276. tabuleiro[2][2] = caractere;
  277. casasVazias -= 1;
  278. }
  279. break;
  280. }
  281.  
  282. return true;
  283. }
  284.  
  285. //Função que imprime o tabueliro atual:
  286. public static void Imprimir_Jogo() {
  287. //Desenhando o tabuleiro:
  288.  
  289.  
  290. System.out.println("|-----------------------------------------------------------------|");
  291. System.out.println("| JOGO DA VELHA |");
  292. System.out.println("|-----------------------------------------------------------------|");
  293. System.out.println();
  294. System.out.println();
  295. System.out.println(tabuleiro[0][0] + " | " + tabuleiro[0][1] + " | "+ tabuleiro[0][2]);
  296. System.out.println("-----------");
  297. System.out.println(tabuleiro[1][0] + " | " + tabuleiro[1][1] + " | "+ tabuleiro[1][2]);
  298. System.out.println("-----------");
  299. System.out.println(tabuleiro[2][0] + " | " + tabuleiro[2][1] + " | "+ tabuleiro[2][2]);
  300. }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement