循环播放井字游戏

编程入门 行业动态 更新时间:2024-10-27 09:33:25
本文介绍了循环播放井字游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我写了一个从头到尾播放井字游戏的代码.游戏结束后,我需要询问用户是否要重复游戏(是/否),并重复直到用户输入否.我不确定在哪里实现循环或如何实现,因此将不胜感激.代码如下:

i wrote a code that plays tic tac toe from start to end. After the game, i need to ask the user if they want to repeat the game (yes/no), and repeat until the user enter no. I'm not sure where to implement the loop or how, so help would be appreciated. the code is as follows:

import java.util.Scanner; public class bb2 { static char place1 = ' '; static char place2 = ' '; static char place3 = ' '; static char place4 = ' '; static char place5 = ' '; static char place6 = ' '; static char place7 = ' '; static char place8 = ' '; static char place9 = ' '; static Scanner input; public static void main(String[] args) { input = new Scanner(System.in); while (true) { // TTT 4 forever! printGameBoard(); userInput(); if (checkForWin('X')) { System.out.println("Congrats, you won!"); break; } if (checkForStale()) { printGameBoard(); System.out.println("Tie!"); break; } computerTurn(); if (checkForWin('O')) { System.out.println("All hail the future overlords!"); break; } if (checkForStale()) { System.out.println("Tie!"); break; } } } static void printGameBoard() { System.out.println(place1 + " | " + place2 + " | " + place3 + "\n---------"); System.out.println(place4 + " | " + place5 + " | " + place6 + "\n---------"); System.out.println(place7 + " | " + place8 + " | " + place9 + "\n"); } static void userInput() { System.out.print("Please enter the board number:"); int place = input.nextInt(); // Note to self: check user input for sanity here! if (checkForSanity(place)) { placeOnBoard(place, 'X'); System.out.println("Ok..."); printGameBoard(); } else { System.out.println("Wrong move!"); } } static void computerTurn() { boolean placed = false; while (!placed) { // Randomly choose a place int place = (int) (1 + (Math.random() * 8)); // Check if sane if (checkForSanity(place)) { placeOnBoard(place, 'O'); placed = true; } } printGameBoard(); } static boolean checkForWin(char piece) { if ((place1 == piece) && (place2 == piece) && (place3 == piece)) return true; if ((place4 == piece) && (place5 == piece) && (place6 == piece)) return true; if ((place7 == piece) && (place8 == piece) && (place9 == piece)) return true; if ((place1 == piece) && (place4 == piece) && (place7 == piece)) return true; if ((place2 == piece) && (place5 == piece) && (place8 == piece)) return true; if ((place3 == piece) && (place6 == piece) && (place9 == piece)) return true; if ((place1 == piece) && (place5 == piece) && (place9 == piece)) return true; if ((place3 == piece) && (place5 == piece) && (place7 == piece)) return true; return false; } static boolean checkForStale() { if ((place1 != ' ') && (place2 != ' ') && (place3 != ' ') && (place4 != ' ') && (place5 != ' ') && (place6 != ' ') && (place7 != ' ') && (place8 != ' ') && (place9 != ' ')) return true; return false; } static void placeOnBoard(int place, char piece) { if (place == 1) place1 = piece; else if (place == 2) place2 = piece; else if (place == 3) place3 = piece; else if (place == 4) place4 = piece; else if (place == 5) place5 = piece; else if (place == 6) place6 = piece; else if (place == 7) place7 = piece; else if (place == 8) place8 = piece; else if (place == 9) place9 = piece; } static boolean checkForSanity(int place) { boolean sane = false; if ((place == 1) && (place1 == ' ')) sane = true; else if ((place == 2) && (place2 == ' ')) sane = true; else if ((place == 3) && (place3 == ' ')) sane = true; else if ((place == 4) && (place4 == ' ')) sane = true; else if ((place == 5) && (place5 == ' ')) sane = true; else if ((place == 6) && (place6 == ' ')) sane = true; else if ((place == 7) && (place7 == ' ')) sane = true; else if ((place == 8) && (place8 == ' ')) sane = true; else if ((place == 9) && (place9 == ' ')) sane = true; return sane; } static boolean checkForSanity(int row, int col) { return true; } }

推荐答案

首先,我建议为所有位置选择一个char数组,而不要选择单个变量.这样,您可以使用循环,而不必在if语句中编写一百万条条件语句.同样,while(true)循环将永远运行而不会清除网格.我建议创建一种方法,以在所有地方都没有' '作为值或有人获胜时检查并返回true,并在while循环中实现该方法.

First I would suggest choosing a char array for all your places instead of individual variables. That way you can use loops instead of writing like a million conditionals in your if statements. Also the while(true) loop will run forever without clearing the grid. I suggest making a method that checks and returns true when none of the places have ' ' as their value or when someone wins, and implement that method in your while loop.

要回答您的原始问题,只需创建一种方法即可清除网格并在主函数开始处实现它,然后将整个对象放入while循环中.

To answer your original question, just make a method that clears the grid and implement it at the start of your main function and then stick the entire thing in a while loop.

更多推荐

循环播放井字游戏

本文发布于:2023-07-11 12:00:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!