Write a game called Cursed Gold. The object of the game is to make your opponent
ID: 3584162 • Letter: W
Question
Write a game called Cursed Gold. The object of the game is to make your opponent select the last gold coin from the pile. There are initially 16 gold coins in the pile and you and your opponent take turns selecting 1.2 or 3 pieces of gold. The person who has to take the last piece from the pile loses. The idea here is to create three functions that represent different strategies. Two of your functions (named playerSimple () and playerSmart ()) are completely automated - think artificial intelligence. They take a single argument - the number of coins currently in the pile. Your functions use this information to decide how to strategize. One of than can be very simple and do the same thing every time but the other function should be a bit more ... intelligent. Random selections are also valid but will only work to a point. These functions print (yes, printing in a function is allowed here) how many coins were removed and they return how many coins ar e left - this is important as this number is needed by the next "player". The third function (playerHuman ()) is a human player. This means that it still takes a single argument - the number of coins remaining but it then prompts the user for input (1.2 and 3 are the only valid inputs) and returns the number of gold pieces remaining. All three of the functions can work in different ways but ultimately, all three will have to deal with the pile of coins in a special way when it is down to three or less - keep this in mind. You'll need a mam function that initializes the pile to 16 gold pieces. It will also be responsible for looping over the player turns, for printing out the number of pieces remaining after each turn and for determining who won. It also determines whether or not the second player is human and calls the appropriate functions. A few hints from my solution (take them or leave them): I have a function called checkWinner() that determines if a winner exists (only 1 coin left!!) and stops the loop. In the output below, my player 1 is my smaller computer player while player 2 is my simpler computer player - only slightly but there is a difference. My solution has a lot of error checking - making sure I don't take too many coins for example - even in the computer players. your game can play human vs. computer only your game can play human vs. human, computer vs. computer or human vs. computerExplanation / Answer
*/ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; public cl*** TicTacToe implements ActionListener { final String VERSION = "2.0"; //Setting up ALL the variables JFrame window = new JFrame("Tic-Tac-Toe " + VERSION); JMenuBar mnuMain = new JMenuBar(); JMenuItem mnuNewGame = new JMenuItem("New Game"), mnuInstruction = new JMenuItem("Instructions"), mnuExit = new JMenuItem("Exit"), mnuAbout = new JMenuItem("About"); JButton btn1v1 = new JButton("Player vs Player"), btn1vCPU = new JButton("Player vs Computer"), btnQuit = new JButton("Quit"), btnSetName = new JButton("Set Player Names"), btnContinue = new JButton("Continue..."), btnTryAgain = new JButton("Try Again?"); JButton btnEmpty[] = new JButton[10]; JPanel pnlNewGame = new JPanel(), pnlMenu = new JPanel(), pnlMain = new JPanel(), pnlTop = new JPanel(), pnlBottom = new JPanel(), pnlQuitNTryAgain = new JPanel(), pnlPlayingField = new JPanel(); JLabel lblTitle = new JLabel("Tic-Tac-Toe"), lblTurn = new JLabel(), lblStatus = new JLabel("", JLabel.CENTER); JTextArea txtMessage = new JTextArea(); final int winCombo[][] = new int[][] { {1, 2, 3}, {1, 4, 7}, {1, 5, 9}, {4, 5, 6}, {2, 5, 8}, {3, 5, 7}, {7, 8, 9}, {3, 6, 9} /*Horizontal Wins*/ /*Vertical Wins*/ /*Diagonal Wins*/ }; final int X = 535, Y = 342, mainColorR = 190, mainColorG = 50, mainColorB = 50, btnColorR = 70, btnColorG = 70, btnColorB = 70; Color clrBtnWonColor = new Color(190, 190, 190); int turn = 1, player1Won = 0, player2Won = 0, wonNumber1 = 1, wonNumber2 = 1, wonNumber3 = 1; int option; boolean inGame = false; boolean win = false; boolean btnEmptyClicked = false; String message, whoTurn, Player1 = "Player 1", Player2 = "Player 2"; public TicTacToe() { //Setting game properties and layout and sytle... //Setting window properties: window.setSize(X, Y); window.setLocation(350, 260); window.setResizable(false); window.setLayout(new BorderLayout()); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Setting Menu, Main, Top, Bottom Panel Layout/Backgrounds pnlMenu.setLayout(new FlowLayout(FlowLayout.CENTER)); pnlTop.setLayout(new FlowLayout(FlowLayout.CENTER)); pnlBottom.setLayout(new FlowLayout(FlowLayout.CENTER)); pnlNewGame.setBackground(new Color(mainColorR - 50, mainColorG - 50, mainColorB- 50)); pnlMenu.setBackground(new Color((mainColorR - 50), (mainColorG - 50), (mainColorB- 50))); pnlMain.setBackground(new Color(mainColorR, mainColorG, mainColorB)); pnlTop.setBackground(new Color(mainColorR, mainColorG, mainColorB)); pnlBottom.setBackground(new Color(mainColorR, mainColorG, mainColorB)); //Setting up Panel QuitNTryAgain pnlQuitNTryAgain.setLayout(new GridLayout(1, 2, 2, 2)); pnlQuitNTryAgain.add(btnTryAgain); pnlQuitNTryAgain.add(btnQuit); //Adding menu items to menu bar mnuMain.add(mnuNewGame); mnuMain.add(mnuInstruction); mnuMain.add(mnuAbout); mnuMain.add(mnuExit);// Menu Bar is Complete //Adding buttons to NewGame panel pnlNewGame.setLayout(new GridLayout(4, 1, 2, 10)); pnlNewGame.add(btnContinue); pnlNewGame.add(btn1v1); pnlNewGame.add(btn1vCPU); pnlNewGame.add(btnSetName); //Setting Button propertied btnTryAgain.setEnabled(false); btnContinue.setEnabled(false); //Setting txtMessage Properties txtMessage.setBackground(new Color(mainColorR-30, mainColorG-30, mainColorB-30)); txtMessage.setForeground(Color.white); txtMessage.setEditable(false); //Adding Action Listener to all the Buttons and Menu Items mnuNewGame.addActionListener(this); mnuExit.addActionListener(this); mnuInstruction.addActionListener(this); mnuAbout.addActionListener(this); btn1v1.addActionListener(this); btn1vCPU.addActionListener(this); btnQuit.addActionListener(this); btnSetName.addActionListener(this); btnContinue.addActionListener(this); btnTryAgain.addActionListener(this); //Setting up the playing field pnlPlayingField.setLayout(new GridLayout(3, 3, 2, 2)); pnlPlayingField.setBackground(Color.black); for(int i=1; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.