Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment you need to write a program using classes provided by the Pr

ID: 3686486 • Letter: F

Question

For this assignment you need to write a program using classes provided by the Processing package (specifically Papplet class, but you may use other classes). Your program has to be interactive and it has to be developed using object oriented programming (i.e. things are represented using classes). Using good OOP practices will be a significant portion of your grade. Implement 'Pong' http://www.ponggame.org/or 'Tic Tac Toe' http://en.wikipedia.org/wiki/Tic-tac-toe. The game should be two player. If you prefer you can create your own game. Talk to me in office hours or after class about your idea. Call your runnable program: MyGame. java. You may pick whatever names you wish for all other classes that you need. What and how to submit? Add the following main() method to the program: public static void main (String args []) { PApplet.main (new String [] { "MyGame"});}

Explanation / Answer

class GAMETicTacToe { static final int EMPTY = 0; static final int NONE = 0; static final int USERONE = 1; static final int COMPUTERONE = 2; public static void main(String[] args) { // CHOOSE FROM THE FOLLOWING OPTIONS 1 = user, 2 = computer int turnplayer = USERONE; // NOW We will be representing the board as nine cells // 0 = empty, 1 = userone, 2 = computerone int[][] board = new int[3][3]; // choose your move: 1-9 representing ul through lr int movehere; //this is to determine the winner: 0 = none, 1 = userone, 2 = computerone, int winnerhere; // Ths is to print Instructions System.out.println("welcome a tic-tac-toe game"); System.out.println("You are playing against the computer!"); System.out.println(" please Enter 1-9 to indicate your move on the board here"); // to print the boards to play the game toprint_board(board); // While loop till the game is not over while(true) { if(turnplayer == USERONE) { System.out.println("This is your move"); movehere = -1; while (movehere9 || board[movehere/3][movehere%3] != EMPTY) { System.out.println(" now Please enter your move(0-9): "); movehere = Console.in.readInt(); Console.in.readChar(); } } else { movehere = performcomputer_move(board); System.out.println("Computer move: " + movehere); } // This is usually done to update the board board[(int)(movehere/3)][movehere%3] = turnplayer; //This is used to call the method toprint_board() toprint_board(board); //now if game is over winnerhere = tocheckWinner(board); if(winnerhere != NONE) break; // switch turn if(turnplayer == USERONE) { turnplayer = COMPUTERONE; } else { turnhere = USERONE; } } // finally we will print out the outcome switch(winner) { case USERONE: System.out.println("You have won!"); break; case COMPUTERONE: System.out.println("The Computer has won!"); break; default: System.out.println("Tie!"); break; } } // This function is used to Print the board public static void toprint_board(int[][] board) { System.out.print(toprintCharhere1(board[0][0])); System.out.print("|"); System.out.print(toprintCharhere1(board[0][1])); System.out.print("|"); System.out.println(toprintCharhere1(board[0][2])); System.out.println("-----"); System.out.print(toprintCharhere1(board[1][0])); System.out.print("|"); System.out.print(toprintCharhere1(board[1][1])); System.out.print("|"); System.out.println(toprintCharhere1(board[1][2])); System.out.println("-----"); System.out.print(tpprintCharhere1(board[2][0])); System.out.print("|"); System.out.print(toprintCharhere1(board[2][1])); System.out.print("|"); System.out.println(toprintCharhere1(board[2][2])); } //This function will return an X or O, depending upon whose move it was public static char toprintChar(int b) { switch(b) { case EMPTY: return ' '; case USERONE: return 'X'; case COMPUTERONE: return 'O'; } return ' '; } // to check the winner and see if the game is over public static int tocheckWinner(int[][] board) { // Check if someone won // Check horizontals // top row if((board[0][0] == board[0][1]) && (board[0][1] == board[0][2])) return board[0][0]; // middle row if((board[1][0] == board[1][1]) && (board[1][1] == board[1][2])) return board[1][0]; // bottom row if((board[2][0] == board[2][1]) && (board[2][1] == board[2][2])) return board[2][0]; // Check verticals // left column if((board[0][0] == board[1][0]) && (board[1][0] == board[2][0])) return board[0][0]; // middle column if((board[0][1] == board[1][1]) && (board[1][1] == board[2][1])) return board[0][1]; // right column if((board[0][2] == board[1][2]) && (board[1][2] == board[2][2])) return board[0][2]; // Check diagonals // one diagonal if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) return board[0][0]; // the other diagonal if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) return board[0][2]; // Check if the board is full if(board[0][0] == EMPTY || board[0][1] == EMPTY || board[0][2] == EMPTY || board[1][0] == EMPTY || board[1][1] == EMPTY || board[1][2] == EMPTY || board[2][0] == EMPTY || board[2][1] == EMPTY || board[2][2] == EMPTY) return NONE; } // now we will generate a random computer move public static int performcomputer_move(int[][] board) { int move1 = (int)(Math.random()*9); while(board[move1/3][move1%3] != EMPTY) move1 = (int)(Math.random()*9); return move1;
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote