Kolko i krzyzyk - prosta wersja

0

Czy ktos umi ten program przeksztalcic bez Apletow , wiem banalne ale potrzebuje to [wstyd]

import java.util.Scanner;

public class TictacToe {

	//Zweidimensionales Array zum Speichern des Spielfelds
	public static int[][] field = new int[3][3];
	
	public static void main(String[] args)
	{
		//Zum Einlesen der Daten
		Scanner s = new Scanner(System.in);
		
		//aktueller Spieler
		int currentPlayer = 1;
		//Gewinner
		int winner = 0;
		
		String x = "";
		int xcoordinate = 0;
		
		String y = "";
		int ycoordinate = 0;
		
		boolean gameFinished = false;
		
		System.out.println("Welcome to Tic Tac Toe");
		System.out.println("right now the board is empty");
		System.out.println("x-direction is from left to right (valid positions:  1,2 or 3)");
		PrintField();
		
		//Solange es keinen Gewinner gibt und das Spiel noch nicht zu Ende ist
		while(winner == 0 && gameFinished == false)
		{
			//Endlosschleife
			for(;;)
			{
				System.out.println("player " + currentPlayer + " enter x-Position");
				x = s.next();
				try
				{
					xcoordinate = Integer.parseInt(x);
					if (xcoordinate >= 1 && xcoordinate <= 3 && RowFree(xcoordinate - 1))
						break;
				}
				catch(Exception e)
				{
					
				}
			}
			
			//Endlosschleife
			for(;;)
			{
				System.out.println("player " + currentPlayer + " enter y-Position");
				y = s.next();
				try
				{
					ycoordinate = Integer.parseInt(y);
					if (ycoordinate >= 1 && ycoordinate <= 3 && FieldFree(xcoordinate - 1,ycoordinate - 1))
						break;
				}
				catch(Exception e)
				{
					
				}
			}
			
			field[xcoordinate - 1][ycoordinate - 1] = currentPlayer;
			
			PrintField();
			
			winner = GetWinner();
			gameFinished = GameFinished();
			
			if (currentPlayer == 1)
				currentPlayer = 2;
			else
				currentPlayer = 1;
		}
		
		if (winner != 0)
			System.out.println("player " + winner + " won this round of TicTacToe");
		else
			System.out.println("There is no winner.");
	}
	
	public static boolean RowFree(int row)
	{
		for (int i = 0; i < 3; i++)
		{
			if (field[row][i] == 0)
				return true;
		}
		
		return false;
	}
	
	public static boolean FieldFree(int row, int column)
	{
		if (field[row][column] == 0)
			return true;
		else
			return false;
	}
	
	//Gibt das Spielfeld aus
	public static void PrintField()
	{
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				if (field[i][j] == 0)
					System.out.print("0");
				else if (field[i][j] == 1)
					System.out.print("X");
				else
					System.out.print("O");
				
				if (j != 2)
					System.out.print("|");
			}
			
			System.out.println();
			
			if (i != 2)
			{
				System.out.println("-+-+-");
			}
		}
	}
	
	public static boolean GameFinished()
	{
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				if (field[i][j] == 0)
					return false;
			}
		}
		
		return true;
	}
	
	public static int GetWinner()
	{
		int winner = 0;
		
		for (int i = 0; i < 3; i++)
		{
			if (field[i][0] != 0 && field[i][0] == field[i][1] && field[i][0] ==
field[i][2])
				winner = field[i][0];
			else if (field[0][i] != 0 && field[0][i] == field[1][i] && field[0][i] == field[2][i])
				winner = field[0][i];
		}
		
		if (winner == 0)
		{
			if (field[0][0] != 0 && field[0][0] == field[1][1] && field[0][0] ==
field[2][2])
				winner = field[0][0];
			else if (field[0][2] != 0 && field[0][2] == field[1][1] && field[0][2] == field[2][0])
				winner = field[0][2];
		}
		
		return winner;
	}
}
0

W tym kodzie nie ma apletow.

0

ale wielkie ooooppppsssss........poczatkowy gamon ze mnie ...... [wstyd]

no w sumie jest z Arrey i funkcjami napisany - hmmm - zaczynam programowac i trudno mi jest przestawic sie z calkiem zwyklego kodu na funkcje.dla niektorych banalne a dla niektorych zrozumiale ...hmmm trzeba bedzie czasu

....Milo ze pikselozo ze to czytales

0

kilka rad na poczatek:

  1. postaraj sie nie uzywac funkcji statycznych (main oczywiscie musi zostac) , sa one oczywiscie przydatne ale nie tutaj.
  2. w funkcji main stworz obiekt klasy TictacToe:
TictacToe ttt=new TictacToe ();

reszte kodu wrzuc do jakiejs funkcji (np. stworz sobie public void start(){...} )
i wywolaj ta metode na rzecz stworzonego obiektu ttt:

ttt.start();

bedze mozna usunac wowczas static z kazdej funkcji poza main ( public static int[][] field tutaj tez )

pzdr.

0

Koszmarnie napisany kod. Lepiej się z takiego nie uczyć.

1 użytkowników online, w tym zalogowanych: 0, gości: 1