Paste: hosey75432

Author: Brandon M. Gerson
Mode: java
Date: Mon, 12 Mar 2012 01:23:33
Plain Text |
import java.util.*;

public class TicTacToe
{
	static int NumWins = 0;
	static int NumLosses = 0;
	static int NumDraws = 0;

	public static void main (String[] args)
	{
	int response = 1; //controls whether to start new game or end game

	while ( response == 1 )
	{
		int SquaresRemaining = 9;
		//Begin Introduction to game
		int SquareNumber[] = new int[10];
		for ( int i = 1; i < SquareNumber.length; i++ )
		{
			SquareNumber[i] = i;
			System.out.printf ( "%2d", i);
			if ( i%3 == 0 )
			{
				System.out.println();
				System.out.println();
			}
		}
		System.out.println ();
		System.out.println ("Welcome to Tic-Tac-Toe. ");
		System.out.println ("First player to place three markers in the same column, row, or diagonal wins.");
		System.out.println ("Enter the square number to submit your move when prompted.");
		//end introduction to game
		System.out.println ();
		System.out.println ( "New Game: " );
		String Square[] = new String[10];
		TicTacToeMethods obj1 = new TicTacToeMethods();
		TicTacToeMethods obj2 = new TicTacToeMethods();
		TicTacToeMethods obj3 = new TicTacToeMethods();
		Scanner input = new Scanner (System.in); //to enter moves
		Scanner input2 = new Scanner ( System.in ); //to play again

		int x = 0;
		int y = 0;
		int DrawCounter = 0;

		while (SquaresRemaining > 0)
		{
		if (SquaresRemaining % 2 == 1)
		{
			System.out.println ("Enter your move (square number): ");
			x = input.nextInt();

			if (x >= 1 && x <= 9)
			{
			if ( Square[x] == null )
			{
				Square[x] = obj1.Markers(x, SquaresRemaining);
				SquaresRemaining -= 1;
				DrawCounter ++;
			}
			else
			System.out.println ("Square unavailable. Enter an open square: ");
			}
			else
			System.out.println ("Square unavailable. Enter an open square: ");

			//print the board after player's move
			for (int i = 1; i < Square.length; i++)
			{
				if ( Square[i] == null )
				System.out.printf ("%2s", " ");
				else
				System.out.printf ("%2s", Square[i]);

				if ( i%3 == 0 )
				{
					System.out.println();
					System.out.println();
				}
			}
		}

		if ( SquaresRemaining % 2 == 0 )
		{
			//use computer logic from methods which blocks threats, attacks, and wins if possible.

			for ( int z = 0; z <= 2; z++ )
			if ( !(Square[z*3+1] == "X" && Square[z*3+2] == "X" && Square [z*3+3] == "X") && !(Square [1+z] == "X" && Square [4+z] == "X" && Square [7+z] == "X") && !(Square [1] == "X" && Square [5] == "X" && Square [9] == "X") && !(Square [3] == "X" && Square [5] == "X" && Square [7] == "X") )
			{
			y = obj2.ComputerLogic( Square );
			Square[y] = obj2.Markers(y, SquaresRemaining);
			System.out.println ("Computer's move: ");

			//print the board after computer's move
			for (int i = 1; i < Square.length; i++)
			{
				if ( Square[i] == null )
				System.out.printf ("%2s", " ");
				else
				System.out.printf ("%2s", Square[i]);

				if ( i%3 == 0 )
				{
					System.out.println();
					System.out.println();
				}
			}
			break;
			}
			else
			{
				break;
			}
			SquaresRemaining -= 1;
		}

		//victory condition

		for ( int i = 0; i <= 2; i++ )
		{
			//horizontal win
			if ( Square[i*3+1] == "X" && Square[i*3+2] == "X" && Square [i*3+3] == "X" )
			{
			SquaresRemaining = 0;
			NumWins ++;
			System.out.printf ( "%5s: %3d %5s: %3d %5s %3s\n", "Wins: ", NumWins, "Losses: ", NumLosses, "Draws: ", NumDraws );
			System.out.println ( "Player wins. Enter 1 to play again, 0 to quit: " );
			response = input2.nextInt();
			if ( response == 1 | response == 0 )
			obj3.PlayAgain(response);
			else
			{
				System.out.println ( "Not a valid choice. Enter 1 to play again, 0 to quit: " );
				response = input2.nextInt();
			}
			}

			//vertical win
			else if ( Square [1+i] == "X" && Square [4+i] == "X" && Square [7+i] == "X" )
			{
			SquaresRemaining = 0;
			NumWins ++;
			System.out.printf ( "%5s: %3d %5s: %3d %5s %3s\n", "Wins: ", NumWins, "Losses: ", NumLosses, "Draws: ", NumDraws );
			System.out.println ( "Player wins. Enter 1 to play again, 0 to quit: " );
			response = input2.nextInt();
			if ( response == 1 | response == 0 )
			obj3.PlayAgain(response);
			else
			{
				System.out.println ( "Not a valid choice. Enter 1 to play again, 0 to quit: " );
				response = input2.nextInt();
			}
			}

			//diagonal win
			else if ( Square [1] == "X" && Square [5] == "X" && Square [9] == "X" )
			{
			SquaresRemaining = 0;
			NumWins ++;
			System.out.printf ( "%5s: %3d %5s: %3d %5s %3s\n", "Wins: ", NumWins, "Losses: ", NumLosses, "Draws: ", NumDraws );
			System.out.println ( "Player wins. Enter 1 to play again, 0 to quit: " );
			response = input2.nextInt();
			if ( response == 1 | response == 0 )
			obj3.PlayAgain(response);
			else
			{
				System.out.println ( "Not a valid choice. Enter 1 to play again, 0 to quit: " );
				response = input2.nextInt();
			}
			}

			else if ( Square [3] == "X" && Square [5] == "X" && Square [7] == "X" )
			{
			SquaresRemaining = 0;
			NumWins ++;
			System.out.printf ( "%5s: %3d %5s: %3d %5s %3s\n", "Wins: ", NumWins, "Losses: ", NumLosses, "Draws: ", NumDraws );
			System.out.println ( "Player wins. Enter 1 to play again, 0 to quit: " );
			response = input2.nextInt();
			if ( response == 1 | response == 0 )
			obj3.PlayAgain(response);
			else
			{
				System.out.println ( "Not a valid choice. Enter 1 to play again, 0 to quit: " );
				response = input2.nextInt();
			}
			}
		}

		for ( int j = 0; j <= 2; j++ )
		{
			if ( Square[j*3+1] == "O" && Square[j*3+2] == "O" && Square [j*3+3] == "O" )
			{
			SquaresRemaining = 0;
			NumLosses ++;
			System.out.printf ( "%5s: %3d %5s: %3d %5s %3s\n", "Wins: ", NumWins, "Losses: ", NumLosses, "Draws: ", NumDraws );
			System.out.println ( "Computer wins. Enter 1 to play again, 0 to quit: " );
			response = input2.nextInt();
			if ( response == 1 | response == 0 )
			obj3.PlayAgain(response);
			else
			{
				System.out.println ( "Not a valid choice. Enter 1 to play again, 0 to quit: " );
				response = input2.nextInt();
			}
			}

			else if ( Square [1+j] == "O" && Square [4+j] == "O" && Square [7+j] == "O" )
			{
			SquaresRemaining = 0;
			NumLosses ++;
			System.out.printf ( "%5s: %3d %5s: %3d %5s %3s\n", "Wins: ", NumWins, "Losses: ", NumLosses, "Draws: ", NumDraws );
			System.out.println ( "Computer wins. Enter 1 to play again, 0 to quit: " );
			response = input2.nextInt();
			if ( response == 1 | response == 0 )
			obj3.PlayAgain(response);
			else
			{
				System.out.println ( "Not a valid choice. Enter 1 to play again, 0 to quit: " );
				response = input2.nextInt();
			}
			}

			else if ( Square [1] == "O" && Square [5] == "O" && Square [9] == "O" )
			{
			SquaresRemaining = 0;
			NumLosses ++;
			System.out.printf ( "%5s: %3d %5s: %3d %5s %3s\n", "Wins: ", NumWins, "Losses: ", NumLosses, "Draws: ", NumDraws );
			System.out.println ( "Computer wins. Enter 1 to play again, 0 to quit: " );
			response = input2.nextInt();
			if ( response == 1 | response == 0 )
			obj3.PlayAgain(response);
			else
			{
				System.out.println ( "Not a valid choice. Enter 1 to play again, 0 to quit: " );
				response = input2.nextInt();
			}
			}

			else if ( Square [3] == "O" && Square [5] == "O" && Square [7] == "O" )
			{
			SquaresRemaining = 0;
			NumLosses ++;
			System.out.printf ( "%5s: %3d %5s: %3d %5s %3s\n", "Wins: ", NumWins, "Losses: ", NumLosses, "Draws: ", NumDraws );
			System.out.println ( "Computer wins. Enter 1 to play again, 0 to quit: " );
			response = input2.nextInt();
			if ( response == 1 | response == 0 )
			obj3.PlayAgain(response);
			else
			{
				System.out.println ( "Not a valid choice. Enter 1 to play again, 0 to quit: " );
				response = input2.nextInt();
			}
			}
		}

		//draw condition
		if ( DrawCounter == 5 )
		{
			NumDraws ++;
			System.out.printf ( "%5s: %3d %5s: %3d %5s %3s\n", "Wins: ", NumWins, "Losses: ", NumLosses, "Draws: ", NumDraws );
			System.out.println ( "Draw. Enter 1 to play again, 0 to quit: " );
			response = input2.nextInt();
			if ( response == 1 | response == 0 )
				obj3.PlayAgain(response);
			else
			{
				System.out.println ( "Not a valid choice. Enter 1 to play again, 0 to quit: " );
				response = input2.nextInt();
			}
		}//end draw condition
		}//end squares remaining loop
		}//end while true loop
		}//end main

}//end class

New Annotation

Summary:
Author:
Mode:
Body: