Answered You can hire a professional tutor to get the answer.

QUESTION

Question: Allow the users to enter the names for player1 and player2 using your game GUI.

Question:I need to help to add following functionality to my java game:

  • If the current player is a winner, update the display label to reflect the status, and write the name of the winner on a new line in the output file named Connect4-Results.txt. You should also and ask if they want to play again. If they decide to NOT play again and terminate this round of games, write to the output file named Connect4-Results.txt the current champion's name and how many games each player won this round. Otherwise, the alternate player should take a turn.
  • If the board is full, check for a winner or a tie. If the game is a tie, indicate that in a seperate line in the output file named Connect4-Results.txt. Ask if the want to play again. If they decide to NOT play again and terminate this round of games, write to the output file named Connect4-Results.txt the current champion's name and how many games each player won this round.

Here's the code for the working game that I build so far:

java.awt.BorderLayout; java.awt.Color; java.awt.Font; java.awt.GridLayout; java.awt.Image; java.awt.event.ActionEvent; java.awt.event.ActionListener; javax.swing.ImageIcon; javax.swing.JButton; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JSlider; javafx.beans.value.ChangeListener; javafx.beans.value.ObservableValue; { TicTacToeBoard playBoard; ScoreBoard scoreBoard; Player player1; Player player2; Player currentPlayer; NUMBER_OF_ROWS = ; NUMBER_OF_COLUMNS = ;ImageIcon blank = ImageIcon(getClass().getResource()); Image blueImage = ImageIcon(getClass().getResource()).getImage().getScaledInstance(, , Image.SCALE_DEFAULT); ImageIcon blue = ImageIcon(blueImage); Image redImage = ImageIcon(getClass().getResource()).getImage().getScaledInstance(, , Image.SCALE_DEFAULT); ImageIcon red = ImageIcon(redImage); {.player1 = Player(, blue);.player2 = Player(, red);.currentPlayer = .player1;setLayout( BorderLayout());playBoard = TicTacToeBoard();add(playBoard, BorderLayout.CENTER);scoreBoard = ScoreBoard();add(scoreBoard, BorderLayout.NORTH);scoreBoard.setCurrentPlayerName(currentPlayer.getPlayerName());setSize(, );setVisible();setDefaultCloseOperation(EXIT_ON_CLOSE);} , { JButton[][] board; {setLayout( GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS));displayBoard();} {.board = JButton[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]; ( row = ; row < .board.length; row++) { ( col = ; col < .board[row].length; col++) {.board[row][col] = JButton();.board[row][col].setEnabled();.board[row][col].setIcon(blank);.board[row][col].addActionListener();add(.board[row][col]);}}} { ( row = ; row < .board.length; row++) { ( col = ; col<.board[row].length; col++) {.board[row][col].setIcon(blank);.board[row][col].setEnabled();}}} {(currentPlayer.equals(player1)) {currentPlayer = player2;} {currentPlayer = player1;}scoreBoard.setCurrentPlayerName(currentPlayer.getPlayerName());} curRow, curCol) { foundCount = ;foundCount = ; ( col = ; col < .board[curRow].length; col++) {ImageIcon buttIcon = (ImageIcon) .board[curRow][col].getIcon(); (buttIcon.equals(icon)) {foundCount++; (foundCount == ) { ;}} {foundCount = ;}}foundCount = ; ( row = ; row < .board.length; row++) {ImageIcon buttonIcon = (ImageIcon) .board[row][curCol].getIcon(); (buttonIcon.equals(icon)) {foundCount++; (foundCount == ) { ;}} {foundCount = ;}} row = curRow; col = curCol; (row != (NUMBER_OF_ROWS -) && col != ) {row++;col--;}foundCount = ;(row > && col<NUMBER_OF_COLUMNS) {ImageIcon buttIcon = (ImageIcon) .board[row][col].getIcon(); (buttIcon.equals(icon)) {foundCount++; (foundCount == ) { ;}} {foundCount = ;}row--;col++;}row = curRow;col = curCol; (row != && col != ) {row--;col--;}foundCount = ;(row < NUMBER_OF_ROWS && col < NUMBER_OF_COLUMNS) {ImageIcon buttIcon = (ImageIcon) .board[row][col].getIcon(); (buttIcon.equals(icon)) {foundCount++; (foundCount == ) { ;}} {foundCount = ;}row++;col++;} ;} {currentPlayer.addNumWins(); numWins = currentPlayer.getNumWins(); (currentPlayer.equals(player1)) {scoreBoard.setPlayer1Score(numWins);} {scoreBoard.setPlayer2Score(numWins);}scoreBoard.setLastWinnerName(currentPlayer.getPlayerName());(player1.compareTo(player2) > ) {scoreBoard.setChampName(player1.getPlayerName());} (player2.compareTo(player1) > ) {scoreBoard.setChampName(player2.getPlayerName());} {scoreBoard.setChampName();}} { yesno = JOptionPane.showConfirmDialog(, , , JOptionPane.YES_OPTION, JOptionPane.NO_OPTION); (yesno == JOptionPane.YES_OPTION) {clearBoard();} {System.exit(EXIT_ON_CLOSE);}} { ( row = ; row < .board.length; row++) { ( col = ; col < .board[row].length; col++) {String buttonText = .board[row][col].getText(); (buttonText.isEmpty()) { ;}}} ;} { ( row = ; row < .board.length; row++) { ( col = ; col < .board[row].length; col++) { (board[row][col].equals(button)) { col;}}} -;} col) { ( row = .board.length -; row >= ; row--) { (board[row][col].getIcon().equals(blank)) { row;}} -;} {JButton clickedButton = (JButton) e.getSource(); col = findColumn(clickedButton); row = findLowestRow(col); (row != - && col != -) {board[row][col].setIcon(currentPlayer.getPlayerIcon()); (isWinner(currentPlayer.getPlayerIcon(), row, col)) {JOptionPane.showMessageDialog(, + currentPlayer.getPlayerName());displayWinner();promptPlayAgain();} (boardIsfull()) {JOptionPane.showMessageDialog(, );promptPlayAgain();}switchCurrentPlayer();}} {}} { JPanel boardSizeInfoJPanel; JLabel numberOfRowsLabel; JSlider numberOfRowSlider; JLabel numberOfColumnsLabel; JSlider numberOfColumns; JPanel genScoreInfo; JLabel labelForChamp, champName; JLabel labelForLastWinner, lastWinnerName; JPanel playScore; JLabel labelPlaceHolder, labelForPlayer1Name, labelForPlayer2Name; JLabel labelForPlayerNames, player1Name, player2Name; JLabel labelForPlayerScore, player1Score, player2Score; JPanel currentPlayerInfo; JLabel labelForCurrentPlayer, currentPlayerName; {setLayout( GridLayout(,));genScoreInfo = JPanel( GridLayout(,));genScoreInfo.setBackground(Color.YELLOW);labelForChamp = JLabel();champName = JLabel();labelForLastWinner = JLabel();lastWinnerName = JLabel();JLabel[] genLabels = {labelForChamp, champName,labelForLastWinner, lastWinnerName}; ( i = ; i < genLabels.length; i++) {genLabels[i].setFont( Font(Font.SANS_SERIF, Font.BOLD, ));genScoreInfo.add(genLabels[i]);}playScore = JPanel( GridLayout(,));playScore.setBackground(Color.CYAN);labelPlaceHolder = JLabel();labelForPlayer1Name = JLabel();labelForPlayer2Name = JLabel();labelForPlayerNames = JLabel();player1Name = JLabel(player1.getPlayerName());player2Name = JLabel(player2.getPlayerName());labelForPlayerScore = JLabel();player1Score = JLabel();player2Score = JLabel();JLabel[] playLabels = {labelPlaceHolder, labelForPlayer1Name, labelForPlayer2Name,labelForPlayerNames,player1Name, player2Name,labelForPlayerScore, player1Score, player2Score}; ( i = ; i < playLabels.length; i++) {playLabels[i].setFont( Font(Font.SANS_SERIF, Font.ITALIC, ));playScore.add(playLabels[i]);}currentPlayerInfo = JPanel( GridLayout(,));currentPlayerInfo.setBackground(Color.LIGHT_GRAY);labelForCurrentPlayer = JLabel();currentPlayerName = JLabel();JLabel[] currentPlayerLabels = {labelForCurrentPlayer, currentPlayerName}; ( i = ; i < currentPlayerLabels.length; i++) {currentPlayerLabels[i].setFont( Font(Font.SANS_SERIF, Font.ITALIC, ));currentPlayerInfo.add(currentPlayerLabels[i]);}add(playScore);add(genScoreInfo);add(currentPlayerInfo);} {.champName.setText(champName);} {.lastWinnerName.setText(lastWinnerName);} score) {.player1Score.setText(+score);} score) {.player2Score.setText(+score);} {.currentPlayerName.setText(currentPlayerName);}}} javax.swing.ImageIcon; <>{ String playerName; ImageIcon playerIcon; numWins; {(, );} {.playerName = playerName;.playerIcon = playerIcon;.numWins = ;} {.numWins++;} String { playerName;} {.playerName = playName;} ImageIcon { playerIcon;} {.playerIcon = playerIcon;} { numWins;} { (obj Player) {Player otherPlayer = (Player) obj;(.playerName.equals(otherPlayer.getPlayerName())) {(.playerIcon.equals(otherPlayer.getPlayerIcon())) {(.numWins == otherPlayer.getNumWins()) { ;}}}} ;} { ;}} { {ConnectX gui = ConnectX();}}
Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question