需要帮助代码。 [英] Need help with code.

查看:54
本文介绍了需要帮助代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直致力于在命令行上使用简单的Tic-Tac-Toe游戏的项目。

代码工作正常但是它的所有java文件都知道这是多么完全不可靠所以我希望将它分解为类,所以我的代码是多个java文件而不是一个。 />


任何人都想帮我解决这个问题。破坏了我的脑袋:)



代码:







Ive been working on a project that is a simple Tic-Tac-Toe game on the command line.
The code works fine but its all one java file and I know how completely unreliable this is so I'm hoping to break it up into classes so my code is numerous java files rather than one .

Anyone want to help me break this up. Been wrecking my head :)

code :



import java.util.Scanner;

public class test3
{
	//Declares co-ordinates of different available moves
	static int A1, A2, A3, B1, B2, B3, C1, C2, C3;
	
	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args)
	{
	System.out.println();
	System.out.println();

	//Prints layout of board so user knows what each section is named
   System.out.println("A1|A2|A3|");
   System.out.println("---------");
   System.out.println("B1|B2|B3|");
   System.out.println("---------");
   System.out.println("C1|C2|C3|");
   
   //Declares both the string for both the player and the computer.
   String prompt = "Welcome to our X and O's game . Please take your first go: ";
		String playerMove = "";
		String cpuMove = "";
		boolean gameIsDecided = false;

		//Because there is only 9 squares , the max amount of plays is 9.
		//The use of the counter keeps track of this
		
		for (int i = 1; i <=9; i++)
		{
			//Gives the human player its identity(1) and sets out what happens if player wins

			playerMove = getMove(prompt);
			updateBoard(playerMove, 1);
			displayBoard();
			if (isGameDecided())
			{
				System.out.println("Congratulations , you have beaten me!!");
				gameIsDecided = true;
				break;
			}

			// Gives the human player its identity(2) and sets out what happens if computer wins
			if (i < 9)
			{
				cpuMove = getCpuMove();
				System.out.println(cpuMove);
				updateBoard(cpuMove, 2);
				displayBoard();
				if (isGameDecided())
				{
					System.out.println("Unlucky , I win. Better luck next time :");
					gameIsDecided = true;
					break;
				}
				prompt = "Take your next go: ";
				i++;
			}
		}
		if (!gameIsDecided)
			System.out.println("So nothing can separate us , the game is a draw !!");
	}

	// This section tests to see if the input from the user is valid. If not alerts user of invalid play
	public static String getMove(String prompt)
	{
		String turn;
		System.out.print(prompt);
		do
		{
			turn = sc.nextLine();
			if (!isAcceptablePlay(turn))
			{
				System.out.println("Unfortunately this move isn't valid , please try again !!");
			}
		} while (!isAcceptablePlay(turn));
		return turn;
	}
//If the play from the user is valid , this section makes sure that the square isnt already occupied then 
//if valid , allows it to be a taken as a play
	public static boolean isAcceptablePlay(String turn)
	{
		if (turn.equalsIgnoreCase("A1") & A1 == 0)
			return true;
		if (turn.equalsIgnoreCase("A2") & A2 == 0)
			return true;
		if (turn.equalsIgnoreCase("A3") & A3 == 0)
			return true;
		if (turn.equalsIgnoreCase("B1") & B1 == 0)
			return true;
		if (turn.equalsIgnoreCase("B2") & B2 == 0)
			return true;
		if (turn.equalsIgnoreCase("B3") & B3 == 0)
			return true;
		if (turn.equalsIgnoreCase("C1") & C1 == 0)
			return true;
		if (turn.equalsIgnoreCase("C2") & C2 == 0)
			return true;
		if (turn.equalsIgnoreCase("C3") & C3 == 0)
			return true;
		return false;
	}

	//This section updates the board in relation to what the user selects 
	public static void updateBoard(String turn, int player)
	{
		if (turn.equalsIgnoreCase("A1"))
			A1 = player;
		if (turn.equalsIgnoreCase("A2"))
			A2 = player;
		if (turn.equalsIgnoreCase("A3"))
			A3 = player;
		if (turn.equalsIgnoreCase("B1"))
			B1 = player;
		if (turn.equalsIgnoreCase("B2"))
			B2 = player;
		if (turn.equalsIgnoreCase("B3"))
			B3 = player;
		if (turn.equalsIgnoreCase("C1"))
			C1 = player;
		if (turn.equalsIgnoreCase("C2"))
			C2 = player;
		if (turn.equalsIgnoreCase("C3"))
			C3 = player;
	}

	//This is responsible for displaying the board on the command line after every go.
	//Each row is declared and using the getSymbol method , the square is filled each turn
	public static void displayBoard()
	{
		String row = "";
		System.out.println();
		
		row = " " + getSymbol(A1) + " | " + getSymbol(A2) + " | " + getSymbol(A3);
		System.out.println(row);
		System.out.println("-----------");
		
		row = " " + getSymbol(B1) + " | " + getSymbol(B2) + " | " + getSymbol(B3);
		System.out.println(row);
		System.out.println("-----------");
		
		row = " " + getSymbol(C1) + " | " + getSymbol(C2) + " | " + getSymbol(C3);
		System.out.println(row);
		System.out.println();
	}
	//Each section is declared as a square and this fills the square with X or O
	//Depending on which person chooses to fill that square.

	public static String getSymbol(int square)
	{
		if (square == 1)
			return "X";
		if (square == 2)
			return "O";
		return " ";
	}
	//This controls the Computer move . By mixing them up we make the computer more competitive
	public static String getCpuMove()
	{
		if (B2 == 0)
			return  "B2";
		
		if (A3 == 0)
			return  "A3";
		
		if (C2 == 0)
			return  "C2";
		
		if (B1 == 0)
			return  "B1";
		
		if (B3 == 0)
			return  "B3";
		
		if (C1 == 0)
			return  "C1";
		
		if (A1 == 0)
			return "A1";
		
		if (C3 == 0)
			return  "C3";
		if (A2 == 0)
			return "A2";
		return "";
	}
	
	//This contains all the possible winning combinations .
	public static boolean isGameDecided()
	{
		if (is3inARow(A1, A2, A3))
			return true;
		if (is3inARow(B1, B2, B3))
			return true;
		if (is3inARow(C1, C2, C3))
			return true;
		if (is3inARow(A1, B1, C1))
			return true;
		if (is3inARow(A2, B2, C2))
			return true;
		if (is3inARow(A3, B3, C3))
			return true;
		if (is3inARow(A1, B2, C3))
			return true;
		if (is3inARow(A3, B2, C1))
			return true;
		return false;
	}

	public static boolean is3inARow(int a, int b, int c)
	{
		return ((a == b) & (a == c) & (a != 0));
	}
	
	
}





我的尝试:



我已经搞砸了它并尝试将其分解但到目前为止我没有运气。如果有人能告诉我如何分解它将会很好



What I have tried:

Ive messed around with it and tried breaking it down but so far I've had no luck. If anyone can show me how I could break it up that would be great

推荐答案

首先通过定义每个类,它包含哪些属性以及什么属性将它分解为单独的类它执行的方法。请参阅课程:类和对象(Java教程 - 学习Java语言) [< a href =http://docs.oracle.com/javase/tutorial/java/javaOO/index.htmltarget =_ blanktitle =New Window> ^ ]。
You break it into separate classes by first defining each class, what properties it contains and what methods it performs. See Lesson: Classes and Objects (The Java Tutorials - Learning the Java Language)[^].


这篇关于需要帮助代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆