Posted April 29, 201114 yr Trying to get it where when you click on the button you can change number.but cant change the original numbersonce the numbers match {4,9,2},{3,5,7},{8,1,6}it says you win do you want to replay import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Lab10 extends JFrame{ private JLabel greeting; private int[][] board = new int [4][4]; private boolean[][] fixed = new boolean [4][4]; private JTextField gameButtons[][] = new JTextField[4][4]; private boolean firstGame = true;private final int window_width =400;private final int window_height=400;static final int[][]winnum = new int[][] {{4,9,2},{3,5,7},{8,1,6}};static final int[][]winnum2 = new int[][] {{8,1,6},{3,5,7},{4,9,2}}; public Lab10() { boolean playAgain = true; boolean gameOver = false; boolean firstGame = true;JFrame frame = new JFrame("Numbers Game");setTitle("Lab 10");setSize(window_width, window_height);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(new GridLayout(4,4));greeting = new JLabel ("Numbers Game");JButton b1 = new JButton("0");JButton b2 = new JButton("9");JButton b3 = new JButton("2");JButton b4 = new JButton("0");JButton b5 = new JButton("0");JButton b6 = new JButton("0");JButton b7 = new JButton("8");JButton b8 = new JButton("0");JButton b9 = new JButton("0");add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9);add(greeting);setVisible(true);for (int r = 1; r <= 9; r++) {if (b1.equals(null)){} } // set up the game. This includes creating all the elements of gameButtons. Each one needs a reasonable Font // so that it looks nice. You also might want to center the text. // Also need to create the button and add the listener to it. // I will let you play with the formatting to make the entire GUI look professional } public static void main(String args[]) { Lab10 game = new Lab10(); new Lab10(); } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { // loop through every element in gameButtons, making sure all of them have some value (length != 0) // and that the number in the textfield is between 1 and 9. You don't have to worry about non-numeric input. // As you get each value from the textfield, set it into the board array. // If every element in gameButtons has a number between 1 and 9 in it, then you can call CheckWinner. This method // is the same as the first lab and uses the board array that you just set. // If there is a winner, then display congratulations and call NextBoard if the player wants to play again. // If there is not a winner, then display a feedback and allow the user to continue } } public boolean CheckForWinner() { // default winner to true, and look for the negative case boolean winner = true; // make sure all numbers are unique. We will do this by creating an array of booleans for each number. A boolean // element of an array defaults to a false, which is what we want. // then we will iterate the array looking at each digit, and setting the respective element of digits true // if we hit a second of any number, then we have a duplicate and so can't have a winner. boolean [] digits = new boolean[10]; for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 3; c++) { if (digits[board[r][c]] == false) { digits[board[r][c]] = true; } else { // here we have a duplicate and so can't have a winner winner = false; } } } if (winner) { // first result is diagnoal up int firstResult = board[1][1] + board[2][2] + board[3][3]; // check diagonal back if ((board[3][1] + board[2][2] + board[1][3]) == firstResult) { // if diagonals are the same, check the rows and columns for (int i = 1; i <= 3; i++) { // check rows if (board[1] + board[2] + board[3] != firstResult) { winner = false; break; } // check columns if (board[1] + board[2] + board[3] != firstResult) { winner = false; break; } } } else { winner = false; } } return winner; } public void NextGame() { // set up the second game by clearing the arrays and then setting the values for the second game. }}
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.