Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Tricky Java Q

Options
  • 02-04-2013 11:55pm
    #1
    Closed Accounts Posts: 5,825 ✭✭✭


    Apologies for the length. Basically I need the code to loop a number of times, defined by the bolded while loop at the top there. Its a simulator of a simple board game. The main problem is that in order for the code to repeat the two players boards (arrays) need to be repopulated with seeds which was attempted in the second bolded point. It wont run more than 4 times tho. If anyone can make head or tails of this and offer a solution ill buy you a set of pogs and a pez dispenser.
    import java.util.ArrayList;
    	import java.util.Random;
    	
    	public class MancalaSir {
    	public static final int Player1Move = 0;
    	public static final int Player2Move = 1;
    	public static final int NULL = 2;
    	public static final int GameOver = 3;
    	public static int gameState = Player1Move;
    	public static player player1, player2;
    	public static void main(String [] arg)
    	{
    	int player1wins = 0;
    	int player2wins = 0;
    	int draws = 0;
    	int a = 0;
    	int tempval = 100;
    	int temp1 = 0;
    	[B]while ( a < tempval)
    	{[/B]
    	//BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    	player1 = new player();
    	player2 = new player();
    	int leftovers = 0;
    	ArrayList <Integer> player1array = new ArrayList<Integer>(1000);
    	ArrayList <Integer> player2array = new ArrayList<Integer>(1000);
    	while (gameState != GameOver)
    	{
    	System.out.println("Player 1 \n ________________");
    	for (int i = 5; i >= 0; i--)
    	System.out.print(" " + player1.board[i] + " ");
    	System.out.println();
    	System.out.println(player1.board[6] + "\t \t " + player2.board[6]);
    	for (int i = 0; i <= 5; i++)
    	System.out.print(" " + player2.board[i] + " ");
    	System.out.println();
    	System.out.println("________________\nPlayer 2");
    	//try
    	//{
    	// codes here
    	if (gameState == Player1Move)
    	{
    	[B]if(gameState == Player1Move && temp1 != 1)
    	{
    	for (int i2 = 0; i2 < 6; i2++)
    	{
    	player1.board[i2] = 1;
    	player1.board[6] = 0;
    
    	for (int j = 0; j < 6; j++)
    	player2.board[j] =1;
    	player2.board[6] = 0;
    	temp1 = 1;
    	}
    	}[/B]
    	int player1move;
    	Random rand1 = new Random();
    	player1move = rand1.nextInt(6); //Integer.parseInt(JOptionPane.showInputDialog("P1:"));
    	System.out.println("\nPlayer 1, Your move:");
    	int h = 0;
    	int i = player1move;
    	if (player1.board[i] != 0)
    	{
    	player1array.add(i);
    	System.out.println(player1array);
    	h = h+1;}
    	else System.out.println("Not a Valid Number try again");
    	if (player1.move(player1move))
    	//if (player1.move(player1move));//Integer.parseInt(reader.readLine())))
    	{
    	leftovers = player1.movingStones;
    	int temppass = 1;
    	while (leftovers > 0)
    	{
    	if (temppass == 1)
    	{
    	player2.receive(leftovers);
    	leftovers = player2.movingStones;
    	player2.capture = -1;
    	}
    	if (temppass == -1)
    	{
    	player1.receive(leftovers);
    	leftovers = player1.movingStones;
    	}
    	temppass *= -1;
    	}
    	// GameOver while
    	if (player1.capture >= 0)
    	{
    	player1.board[6] += player1.board[player1.capture];
    	player1.board[player1.capture] = 0;
    	player1.board[6] += player2.board[5 - player1.capture];
    	player2.board[5 - player1.capture] = 0;
    	player1.capture = -1;
    	}
    	// check if the game finished
    	if (player1.gamefinished())
    	{
    
    	int player1score = player1.board[6];
    	int player2score = player2.board[6] + player2.board[0] + player2.board[1] + player2.board[2] + player2.board[3] + player2.board[4] + player2.board[5];
    	if (player1score > player2score)
    	{
    	player1wins = player1wins +1;
    	System.out.println("Player 1 wins");
    	}
    	else if (player1score == player2score)
    	{
    	draws = draws + 1;
    	System.out.println("Draw");
    	}
    	else
    
    	{ 
    	player2wins = player2wins + 1;
    	System.out.println("Player 2 wins");
    	}
    	System.out.println("Player 1 \n ________________");
    	for (int lol = 5; lol >= 0; lol--)
    	System.out.print(" " + player1.board[lol] + " ");
    	System.out.println();
    	System.out.println(player1.board[6] + "\t\t " + player2.board[6]);
    	for (int lol = 0; lol <= 5; lol++)
    	System.out.print(" " + player2.board[lol] + " ");
    	System.out.println();
    	System.out.println("________________\nPlayer 2");
    	gameState = GameOver;
    	}
    	else
    	{
    	if (player1.again)
    	player1.again = false;
    	else
    	gameState = Player2Move;
    	}
    	}
    	else{
    	System.out.println("Player 1 please move again:\n");
    	}
    	}
    	//***********************************************************************
    	/*
    	*/
    	else if (gameState == Player2Move)
    	{
    	int player2move;
    	Random rand = new Random();
    	player2move = rand.nextInt(6); //Integer.parseInt(JOptionPane.showInputDialog("P2:"));
    	System.out.println("\nPlayer Two, Your move:");
    	int j = 0;
    	int k = player2move;
    	if (player2.board[k] != 0)
    	{
    	player2array.add(k);
    	j = j + 1;
    	System.out.println(player2array);
    	}
    	else System.out.println("Not a Valid Number");
    	if (player2.move(player2move)) //rand.next(reader.readLine())))
    	{
    	leftovers = player2.movingStones;
    	int temppass = -1;
    	while (leftovers > 0)
    	{
    	if (temppass == 1)
    	{
    	player2.receive(leftovers);
    	leftovers = player2.movingStones;
    	}
    	if (temppass == -1)
    	{
    	player1.receive(leftovers);
    	leftovers = player1.movingStones;
    	player1.capture = -1;
    	}
    	temppass *= -1;
    	}
    	// GameOver while
    	if (player2.capture >= 0)
    	{
    	player2.board[6] += player2.board[player2.capture];
    	player2.board[player2.capture] = 0;
    	player2.board[6] += player1.board[5 - player2.capture];
    	player1.board[5 - player2.capture] = 0;
    	player2.capture = -1;
    	}
    	// check if the game finished
    	if (player2.gamefinished())
    
    	{
    	int player1score = player1.board[6] + player1.board[0] + player1.board[1] + player1.board[2] + player1.board[3] + player1.board[4] + player1.board[5];
    	int player2score = player2.board[6];
    	if (player2score > player1score)
    	{
    	player2wins = player2wins + 1;
    	System.out.println("Player 2 wins");
    	}
    	else if (player2score == player1score)
    	{
    	draws = draws + 1;
    	System.out.println("Draw");
    	}
    	else
    	player1wins = player1wins + 1;
    	System.out.println("Player 1 wins");
    	gameState = GameOver;
    	}
    	else
    	if (player2.again)
    	player2.again = false;
    	else
    	{
    	gameState = Player1Move;
    	}
    	}
    	else
    	System.out.println("Player 2 please move again:\n");
    	}
    	a= a + 1;
    	}
    	temp1 = 0;
    	// GameOver if
    	gameState = Player1Move;
    	}
    	System.out.println("Player 1 wins " + player1wins);
    	System.out.println("Player 2 wins " + player2wins);
    	System.out.println("Draws " + draws);
    	// End of while
    	/*for (int i = 0; i < 6; i++)
    	player1.board[i] = 1;
    	player1.board[6] = 0;
    
    	for (int j = 0; j < 6; j++)
    	player2.board[j] =1;
    	player2.board[6] = 0;*/
    
    	}// End of main
    	// endwhile
    	} // End of class
    

    EDIT: Methods and some class definitions are in the same main below this code if anyone needs a look at it I can post :pac:
    Tagged:


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Argh my eyes, fix indentation first please :P


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    srsly78 wrote: »
    Argh my eyes, fix indentation first please :P

    Well that.... and use some bloody methods!! :D


  • Closed Accounts Posts: 1,235 ✭✭✭returnNull


    probably shouldnt call an int variable null.Just causes confusion if you assign null to an object.

    Your IDE should have a short cut for indentation.Its unreadable at the moment as a poster above said,not having a go at you,it just makes it readable and easier to troubleshoot.


  • Closed Accounts Posts: 816 ✭✭✭Opinicus


    Also your player class should be called Player, with a capital 'P'. Was very confusing to read through it.
    Post up the rest of the code if you really want someone to help.


Advertisement