I'm new to Java so I'm sure my code is abysmal in comparison to cleaner/organized code.
Link to full code: http://pastebin.com/UXJzU2ax
What it looks like when ran.
Image
I'm attempting to add a number onto the end of a variable via a number in an array. I'm unsure as to how this can be achieved.
I have created a 10x10 grid of buttons with randomly generated colors, I wish to change the adjacent buttons color on click, but only if the color is the same.
Color generation.
int[] squares;
squares = new int[101];
for (int i = 1; i <= 100; i++){
squares[i] = i;
JButton button = new JButton("" + squares[i]);
int color = (int) (Math.random() * 4); //rand 1 to 4
if (color == 1){
button.setBackground(Color.red);
} else if (color == 2){
button.setBackground(Color.green);
} else if (color == 3){
button.setBackground(Color.yellow);
} else {
button.setBackground(Color.blue);
}
button.setName("button" + (Integer.toString(squares[i])));
button.addActionListener(new ActionListener()
{
My way of knowing where directions are.
right = Integer.parseInt(button.getText()) + 1;
up = Integer.parseInt(button.getText()) - 10;
down = Integer.parseInt(button.getText()) + 10;
right2 = squares[right];
up2 = squares[up];
down2 = squares[down];
This is where I'm running into troubles.
if (btnSelectedColor.getBackground() == button.getBackground()){
//Where I need the change in "button", like "button[right].getBackground", or something similar.
if (button.getBackground() == btnSelectedColor.getBackground()){
}
}
Here's how you can access the other buttons.
Note: I will not write all the logic here. I will leave the method of getting the neighbors to you.
In the actionPerformed callback, you need somehting like this:
public void actionPerformed(ActionEvent e)
{
// get all components of the panel
Component[] comp = panel.getComponents();
// loop through all components
for (int i = 0; i < comp.length; i++)
{
// if it's a button...
if (comp[i] instanceof JButton)
{
JButton b = (JButton) comp[i];
// if b is a neighbor of button (e.g. the one clicked),
// then change its background color
// I leave this logic to you (seems like you wrote most if it already)
// Hint: Convert the text of b to an int and convert the
// text of button to an int and compare the ints to see if
// they are neighbors. You can convert the int of
// button.getText once outside of the loop. No sense in
// doing the conversion every time since it will be the same.
}
}
// Rest of code in your actionPerformed method...
}
Good luck!
I would extend JButton
public class BuddyButton extends JButton {
The new BuddyButton would have 4 fields
private BuddyButton north;
private BuddyButton south;
private BuddyButton east;
private BuddyButton west;
Initerate buttons[][]
BuddyButton buttons[][] = new BuddyButton[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
BuddyButton buddy = new BuddyButton();
buttons[i][j] = buddy;
panel.add(buddy);
buddy.addActionListener(listener);;
}
}
Loop through buttons[][] again linking all the BuddyButtons
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
BuddyButton buddy = new BuddyButton();
buttons[i][j] = buddy;
panel.add(buddy);
buddy.addActionListener(listener);
}
}
public static void linkBuddies(int i, int j) {
if (i > 0)
buttons[i][j].setNorth(buttons[i - 1][j]);
if (i < 10)
buttons[i][j].setSouth(buttons[i + 1][j]);
if (j > 0)
buttons[i][j].setWest(buttons[i + 1][j - 1]);
if (j > 10)
buttons[i][j].setEast(buttons[i + 1][j + 1]);
}
When a BuddyButton is clicked cast e.getSource() back to a BuddyButton
listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
BuddyButton buddy = (BuddyButton) e.getSource();
BuddyButton north = buddy.getNorth();
BuddyButton south = buddy.getSouth();
BuddyButton east = buddy.getEast();
BuddyButton west = buddy.getWest();
//Check for nulls and talk to the neighbors!!!!
}};
I don't know why I am incorrect in my bounds and why this error is thrown.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
private int gridSize = 3;
private Point currentStep = new Point(0, 0);
private Point firstStep = new Point(0, 0);
private Point lastStep = new Point(gridSize, gridSize);
private int pedometer = 0;
private int random;
private int down = 0;
private int right = 0;
private byte bottomReached = 0;
private byte rightReached = 0;
private int[][] clearPath2D;
public void createWalk2D() {
clearPath2D = new int[gridSize][gridSize];
for (currentStep = firstStep; currentStep != lastStep; pedometer++) {
step2D();
if (rightReached == 1 && bottomReached == 1) {
break;
}
}
clearField();
}
public void step2D() {
random = stepRand.nextInt();
// add a new step to the current path
currentStep.setLocation(right , down);
clearPath2D[right][down] = 4;
// calculates the next step based on random numbers and weather a side
// is being touched
if (currentStep.x == gridSize) {
rightReached = 1;
random = 1;
}
if (currentStep.y == gridSize) {
bottomReached = 1;
random = 0;
}
// decides the direction of the next step
if (random >= 0.5 && bottomReached == 0) {
down++;
} else if (random < 0.5 && rightReached == 0) {
right++;
} else if (rightReached == 1 && bottomReached == 1) {
done = true;
}
}
so I call the createWalk2D(); then I get the error and eclipse points me to this line of code:
clearPath2D[right][down] = 4;
I assume this is because I am looping incorreclty. I have not been able to find a solution and have googled for about an hour three different days.
this isn't all the code but this is the part that I think is throwing it off. Thank you in advance for any help with the error. If you need the whole code please let me know.
EDIT:
Nevermind I figured it out.
I had to add 1 to the initial declaration of the array
in this case it meant changing
clearPath2D = new int[gridSize][gridSize];
to
clearPath2D = new int[gridSize + 1][gridSize + 1];
Your immediate problem is in this section of code:
if (currentStep.x == gridSize) {
rightReached = 1;
random = 1;
}
if (currentStep.y == gridSize) {
bottomReached = 1;
random = 0;
}
You should be testing against gridSize-1, since that is the maximum valid index. As in:
if (currentStep.x == gridSize-1) {
rightReached = 1;
random = 1;
}
if (currentStep.y == gridSize-1) {
bottomReached = 1;
random = 0;
}
This question already has an answer here:
Java: GUI - JButton opening new JPanel and reading the JTextFields
(1 answer)
Closed 9 years ago.
My problem is making my Main class and Journal class connect together, in the Main class users will input two variables and click the button to create the table, in the Journal class, it will take the two variables and make it go through a process before they get added into the table so i need JTextField1 as variable a and JTextField2 as variable b.
I used netbeans to create the Main class and my own methods to create the table class please help! Thanks! This is the part of the Main class Netbeans tells me to edit when I right click > Events > Action > Action Performed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int data1 = Integer.parseInt(jTextField1.getText());
int data2 = Integer.parseInt(jTextField2.getText());
}
Here is my Code:
package Components;
/**
*
* #author dustinpx2014
*/
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class Journal extends JPanel
{
private JTable table;
private int a;//Number of Students
private int b;// Length of Trip
public Journal()
{
String colN1 = "Date";
String colN2 = "Student";
int c = a*2/b; // Determining # of Journals
int col = c*2; // Creating two columns for every journal(Day and Student)
String[] columnNames = new String[col]; //For Loop: inserting column names
for(int colF = 0; colF < col; colF++)
{
if(colF%2 == 0)
{
columnNames[colF] = colN1;
}
else
{
columnNames[colF] = colN2;
}
}
int row = b; //row = number of days
int d = 1; //day number
int s = 1; //student number
int x = 0; //counter for the # of times students write
int z = 1; // student number(no limit)
int z1 = a/2; // student number 2
int x1 = 0; // counter for the # of times students write 2
int x2 = 0;
int w = 1;
Object[][] data = new Object[row][col];
for (int col1 = 0; col1 < data[0].length; col1++)
{
for (int row1 = 0; row1<data.length; row1++)//goes through the table by column
{
if(d > b) //reseting the date
{
d = 1;
}
if(s > a && x <= 2) //reseting student number and adds count
{
s = 1;
x++;
}
if(z > a)
{
z = 1;
}
if(z1 > a && x1 <= 2)
{
z1 = 1;
x1++;
}
if (w>a)
{
w++;
}
if(col1%2 == 0) //inserts the day
{
data[row1][col1]= "Day " + d;
d++;
}
else if (s!=data[row1][col1])//inserts student number
{
data[row1][col1]= s;
s++;
z++;
w++;
}
for (int col2 = 1; col2 < data[0].length; col2++)
{
for (int row2 = 0; row2<data.length; row2++)//goes through the table by column
{
if(z == data[row2][col2] && col2%2!=0)//checks for repeats and replaces them
{
for (int y = 0; y < col; y++) //checking all columns
{
if (z1 == a/2 && x1 <= 5) //adds count
{
x1++;
}
else if(z1 > a && x1 <= 5)
{
z1 = 1;
x1++;
}
if(z == data[row2][y])
{
data[row2][col2] = z1;
z1++;
w++;
}
}
}
}
}
for (int row3 = 1; row3 < data.length; row3++)
{
for (int col3 = 0; col3<data[0].length; col3++)//goes through the table by rows
{
if(w == data[row3][col3] && col3%2!=0)//checks for repeats and replaces them
{
for(int y2 = 0; y2 < col; y2++)
{
if(
row3<row-1 &&
row3> 1 &&
w==data[row3+1][y2] &&
w==data[row3-1][y2] &&
w==data[row3][y2]
) //checks rows
{
data[row3][col3] = 0;
}
}
}
}
}
}
}
JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
public JTable getTable() {
return table;
}
private static void gui()
{
JFrame gui = new JFrame();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Journal List");
gui.setSize(700,200);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
gui.add(new Journal());
}
}
Please help add anything or delete anything in my code to make them work, THANK YOU :)
write that over your constrictor
public static void main(String args[]){
Journal is= new Journal();
}
public Journal()
{...}
I am making a tic tac toe game for n number of players on a nxn board, but the winning condition is aways 3 on a row. My so far solution to the problem is: when a move is made the program will check the following square for 3 on a row.
(x-1,y+1) (x,y+1) (x+1,y+1)
(x-1,y) (x,y) (x+1,y)
(x-1,y-1) (x,y-1) (x+1,y-1)
It will check the top (x-1,y+1) (x,y+1) (x+1,y+1) bottom(x-1,y-1) (x,y-1) (x+1,y-1)
sides(x+1,y+1) (x+1,y) (x+1,y-1) , (x-1,y+1) (x-1,y) (x-1,y-1) , the diagonals and the ones going through the middle(x,y).
my code so far is:
public int checkWinning() {
for(int a = 1; a < size-1; a++){
for(int b = 1; b < size-1; b++){
if (board[a][b] == board[a+1][b] && board[a][b] == board[a-1][b]){
return board[a][b];
}else if(board[a][b] == board[a][b+1] && board[a][b] == board[a][b-1]){
return board[a][b];
}else if(board[a][b] == board[a+1][b-1] && board[a][b] == board[a-1][b+1]){
return board[a][b];
}else if(board[a][b] == board[a+1][b+1] && board[a][b] == board[a-1][b-1]){
return board[a][b];
}
}
}
for(int d = 1; d < size-1; d++){
if (board[0][d] == board[0][d-1] && board[0][d] == board[0][d+1]){
return board[0][d];
} else if (board[size-1][d] == board[size-1][d-1] && board[size-1][d] == board[size-1][d+1]){
return board[size-1][d];
}
}
for(int c = 1; c < size-1; c++){
if (board[c][0] == board[c-1][0] && board[c][0] == board[c+1][0]){
return board[c][0];
}else if(board[c][size-1] == board[c-1][size-1] && board[c][size-1] == board[c+1][size-1]){
return board[c][size-1];
}
}
return 0;
}
where the first section is where I check the ones through the middle and diagonals. the second section I check the top an bottom and the top and the thrid section checks the sides.
When it returns 0 is means that there are no winner yet.
#override
public void checkResult() {
int winner = this.board.checkWinning();
if (winner > 0) {
this.ui.showResult("Player "+winner+" wins!");
}
if (this.board.checkFull()) {
this.ui.showResult("This is a DRAW!");
}
}
Board[x][y] -> 2-dimensional array representing the board, The coordinates are counted from top-left (0,0) to bottom-right (size-1, size-1), board[x][y] == 0 signifies free at position (x,y), board[x][y] == i for i > 0 signifies that Player i made a move on (x,y), just so you know it.
my problem is that when i expands the board to a size larger than 3x3 the program somehow overwrites it self or a does not check every thing sides top and bottom every time, and I can't seem too se why.
EDIT:
played with the app for a few minutes... interesting results
java -jar tic-tac-toe.jar 5 20
It was a cats game!!
|1|1|5|5|1|3|5|3|1|5|2|5|1|1|2|
|2|3|2|3|1|5|3|5|3|2|3|1|5|2|2|
|5|4|5|4|1|5|5|4|2|1|4|5|4|2|2|
|3|2|1|5|5|5|2|4|5|3|4|1|2|4|2|
|3|4|1|2|5|4|1|1|4|5|1|3|3|4|1|
|1|5|4|4|3|2|5|1|3|5|1|3|5|3|4|
|2|5|1|4|3|3|3|5|3|1|1|4|3|4|4|
|1|4|5|1|1|5|4|5|2|4|1|1|5|4|3|
|1|3|2|1|4|2|4|3|3|4|5|2|4|3|3|
|5|1|1|3|3|4|4|4|2|2|1|4|3|2|5|
|2|2|3|1|5|5|4|1|3|5|3|2|3|3|2|
|2|4|2|4|4|1|3|1|1|3|1|2|1|2|2|
|2|5|5|1|4|3|4|5|5|4|5|3|3|5|2|
|4|5|2|1|5|3|2|1|3|2|2|2|2|4|4|
|4|1|1|4|5|4|5|4|2|2|3|3|2|2|3|
Played 100 games:
Number wins by Player1: 0
Number wins by Player2: 0
Number wins by Player3: 0
Number wins by Player4: 0
Number wins by Player5: 0
Number of ties: 100
didn't scroll through all 100 games to find the winning board, but I thought this was interesting:
java -jar tic-tac-toe.jar 2 10
Player2 won the game!
|1|1|2|1|2|2| |2|1|2|
|2|2|2|2|2|2|2|2|2|2|
|2|1|2|2|2|1|1|1|1|1|
|1|1|1|1|2|1|2|1|1|1|
|2|2| |1|2|1|1|1|1|2|
|2|2|2|1|1|1| |1|2|2|
|2|2|1|2|2|2|2|2|1|1|
| | |2|2|2|2| |1|1|1|
|1|1|2|2|2|1|1|1|1| |
| | |1|1|1|1|1|2|1| |
Played 100 games:
Number wins by Player1: 0
Number wins by Player2: 1
Number of ties: 99
This does answer your question... but I took it a bit far... decided to implement the solution.
Instead of counting matches... I just check from teh point the last player plays, if all marks in a row column and diagnal match the players, he wins.
package com.clinkworks.example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TicTacToe {
private static final String TIE = "TIE";
private static final Map<String, Integer> gamesToWinsMap = new HashMap<String, Integer>();
/**
* accepts input in the following format:
*
* playerCount rowCount columnCount (sets the game with the n players, n columns, and n rows)
* - java -jar tic-tac-toe.jar 2 3 3
* PlayerCount squareSize (defaults to a game with rows and cols the same as squareSize and the player count given)
* - java -jar tic-tac-toe.jar 2 3
* PlayerCount (defaults to a 3 by 3 game)
* - java -jar tic-tac-toe.jar 2
* no input (defaults to a 3 by 3 game with 2 players)
* - java -jar tic-tac-toe.jar
* #param args
*/
public static void main(String[] args) {
int playerCount = 2;
int rows = 3;
int cols = 3;
if(args.length == 3){
playerCount = Integer.valueOf(args[0]);
rows = Integer.valueOf(args[1]);
cols = Integer.valueOf(args[2]);
}
if(args.length == 2){
playerCount = Integer.valueOf(args[0]);
rows = Integer.valueOf(args[1]);
cols = rows;
}
if(args.length == 1){
playerCount = Integer.valueOf(args[0]);
}
for(int i = 1; i <= playerCount; i++){
gamesToWinsMap.put("Player" + i, 0);
}
//lets play 100 games and see the wins and ties
playGames(100, playerCount, rows, cols);
for(int i = 1; i <= playerCount; i++){
System.out.println("Number wins by Player" + i + ": " + gamesToWinsMap.get("Player" + i));
}
System.out.println("Number of ties: " + gamesToWinsMap.get(TIE));
}
public static void playGames(int gamesToPlay, int playerCount, int rows, int cols) {
//play a new game each iteration, in our example, count = 100;
for (int i = 0; i < gamesToPlay; i++) {
playGame(playerCount, rows, cols);
}
}
public static void playGame(int playerCount, int rows, int cols) {
//create a new game board. this initalizes our 2d array and lets the complexity of handling that
// array be deligated to the board object.
Board board = new Board(playerCount, rows, cols);
//we are going to generate a random list of moves. Heres where we are goign to store it
List<Move> moves = new ArrayList<Move>();
//we are creating moves for each space on the board.
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
moves.add(new Move(row, col));
}
}
//randomize the move list
Collections.shuffle(moves);
//do each move
for (Move move : moves) {
board.play(move);
if(gameOver(board)){
break;
}
}
}
public static boolean gameOver(Board board){
if (board.whoWon() != null) {
System.out.println(board.whoWon() + " won the game!");
System.out.println(board);
Integer winCount = gamesToWinsMap.get(board.whoWon());
winCount = winCount == null ? 1 : winCount + 1;
gamesToWinsMap.put(board.whoWon(), winCount);
return true;
} else if (board.movesLeft() == 0) {
System.out.println("It was a cats game!!");
System.out.println(board);
Integer tieCount = gamesToWinsMap.get(TIE);
tieCount = tieCount == null ? 1 : tieCount + 1;
gamesToWinsMap.put(TIE, tieCount);
return true;
}
return false;
}
public static class Move {
private int row;
private int column;
public Move(int row, int column) {
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}
public static class Board {
private final int rowSize;
private final int columnSize;
private final Integer[][] gameBoard;
private int playerCount;
private int currentPlayer;
private String winningPlayer;
public Board() {
gameBoard = new Integer[3][3];
currentPlayer = 1;
winningPlayer = null;
this.rowSize = 3;
this.columnSize = 3;
playerCount = 2;
}
public Board(int players) {
gameBoard = new Integer[3][3];
currentPlayer = 1;
winningPlayer = null;
this.rowSize = 3;
this.columnSize = 3;
playerCount = players;
}
public Board(int rowSize, int columnSize) {
gameBoard = new Integer[rowSize][columnSize];
currentPlayer = 1;
winningPlayer = null;
playerCount = 2;
this.rowSize = rowSize;
this.columnSize = columnSize;
}
public Board(int players, int rowSize, int columnSize) {
gameBoard = new Integer[rowSize][columnSize];
currentPlayer = 1;
winningPlayer = null;
playerCount = players;
this.rowSize = rowSize;
this.columnSize = columnSize;
}
/**
*
* #return the amount of empty spaces remaining on the game board, or if theres a winning player, zero.
*/
public int movesLeft() {
if(whoWon() != null){
return 0;
}
int moveCount = 0;
for (int x = 0; x < getRowSize(); x++) {
for (int y = 0; y < getColumnSize(); y++) {
moveCount += getMoveAt(x, y) == null ? 1 : 0;
}
}
return moveCount;
}
/**
* If someone won, this will return the winning player.
*
* #return the winning player
*/
public String whoWon() {
return winningPlayer;
}
/**
* This move allows the next player to choose where to place their mark.
*
* #param row
* #param column
* #return if the game is over, play will return true, otherwise false.
*/
public boolean play(Move move) {
if (!validMove(move)) {
// always fail early
throw new IllegalStateException("Player " + getCurrentPlayer() + " cannot play at " + move.getRow() + ", " + move.getColumn() + "\n" + toString());
}
doMove(move);
boolean playerWon = isWinningMove(move);
if (playerWon) {
winningPlayer = "Player" + getCurrentPlayer();
return true;
}
shiftPlayer();
boolean outOfMoves = movesLeft() <= 0;
return outOfMoves;
}
public int getRowSize() {
return rowSize;
}
public int getColumnSize() {
return columnSize;
}
public int getCurrentPlayer() {
return currentPlayer;
}
public Integer getMoveAt(int row, int column) {
return gameBoard[row][column];
}
private void doMove(Move move) {
gameBoard[move.getRow()][move.getColumn()] = getCurrentPlayer();
}
private void shiftPlayer() {
if(getCurrentPlayer() == getPlayerCount()){
currentPlayer = 1;
}else{
currentPlayer++;
}
}
private int getPlayerCount() {
return playerCount;
}
private boolean validMove(Move move) {
boolean noMoveAtIndex = false;
boolean indexesAreOk = move.getRow() >= 0 || move.getRow() < getRowSize();
indexesAreOk = indexesAreOk && move.getColumn() >= 0 || move.getColumn() < getColumnSize();
if (indexesAreOk) {
noMoveAtIndex = getMoveAt(move.getRow(), move.getColumn()) == null;
}
return indexesAreOk && noMoveAtIndex;
}
private boolean isWinningMove(Move move) {
// since we check to see if the player won on each move
// we are safe to simply check the last move
return winsDown(move) || winsAcross(move) || winsDiagnally(move);
}
private boolean winsDown(Move move) {
boolean matchesColumn = true;
for (int i = 0; i < getColumnSize(); i++) {
Integer moveOnCol = getMoveAt(move.getRow(), i);
if (moveOnCol == null || getCurrentPlayer() != moveOnCol) {
matchesColumn = false;
break;
}
}
return matchesColumn;
}
private boolean winsAcross(Move move) {
boolean matchesRow = true;
for (int i = 0; i < getRowSize(); i++) {
Integer moveOnRow = getMoveAt(i, move.getColumn());
if (moveOnRow == null || getCurrentPlayer() != moveOnRow) {
matchesRow = false;
break;
}
}
return matchesRow;
}
private boolean winsDiagnally(Move move) {
// diagnals we only care about x and y being teh same...
// only perfect squares can have diagnals
// so we check (0,0)(1,1)(2,2) .. etc
boolean matchesDiagnal = false;
if (isOnDiagnal(move.getRow(), move.getColumn())) {
matchesDiagnal = true;
for (int i = 0; i < getRowSize(); i++) {
Integer moveOnDiagnal = getMoveAt(i, i);
if (moveOnDiagnal == null || moveOnDiagnal != getCurrentPlayer()) {
matchesDiagnal = false;
break;
}
}
}
return matchesDiagnal;
}
private boolean isOnDiagnal(int x, int y) {
if (boardIsAMagicSquare()) {
return x == y;
} else {
return false;
}
}
private boolean boardIsAMagicSquare() {
return getRowSize() == getColumnSize();
}
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
for(int y = 0; y < getColumnSize(); y++) {
for(int x = 0; x < getRowSize(); x++) {
Integer move = getMoveAt(x, y);
String moveToPrint = "";
if (move == null) {
moveToPrint = " ";
} else {
moveToPrint = move.toString();
}
stringBuffer.append("|").append(moveToPrint);
}
stringBuffer.append("|\n");
}
return stringBuffer.toString();
}
}
}
I have to revise my answer. If you want to have three in a row regardless of your board size, your loop code might be sufficient, but you are always checking whether the values of the fields are the same but never make a difference between empty and non-empty fields.
So “empty” can win too, which would effectively hide a possible win of a player. In other words, your code does not work correctly, even for a field size of three. You didn’t test it enough.
If I initialize the board as
int[][] board={
{ 1, 1, 1 },
{ 0, 0, 0 },
{ 0, 0, 0 },
};
your code returns 0 as the second row contains three zeros. I assumed that 0 represents the empty field but the actual value for “empty” doesn’t matter. You have to exclude empty fields from the three-in-a-row check.
You can simplify this a fair amount by breaking the logic up a bit.
First realize that you only need to check for a win around the piece you just placed.
Now we need a way to check whether that move is a winner.
First we need a simple function to check whether a cell matches a given value, returning true if its within bounds and matches.
private boolean cellMatches(int x, int y, int val) {
if (x<0||x>boardWidth)
return false;
if (y<0||y>boardHeight)
return false;
return board[x][y]==val;
}
Now a function that you give a starting position (x and y) and a delta (dx, dy) and it checks up to two cells in that direction returning a count of how many in a row matched value. The for loop may be overkill for two checks but it would easily allow you to expand up to longer lines being used.
private int countMatches(int x, int y, int dx, int dy, int val) {
int count = 0;
for (int step=1;step<=2;step++) {
if (cellMatches(x+dx*step, y+dy*step, val) {
count++;
} else {
return count;
}
}
return count;
}
Now we can use the previous method. When we place a new piece we can just count out in each matching pair of directions. The combined count is the total number in a row. (i.e. two in a row top + 1 bot = a total run length of 4). If any of those run lengths is three then it is a winning move.
private boolean makeMove(int x, int y, int val) {
board[x][y] = val;
int runlength=countMatches(x,y,0,1,val) + countMatches(x,y,0,-1,val);
if (runLength >= 2)
return true;
int runlength=countMatches(x,y,1,0,val) + countMatches(x,y,-1,0,val);
if (runLength >= 2)
return true;
int runlength=countMatches(x,y,1,1,val) + countMatches(x,y,-1,-1,val);
if (runLength >= 2)
return true;
int runlength=countMatches(x,y,1,-1,val) + countMatches(x,y,-1,1,val);
if (runLength >= 2)
return true;
return false;
}
Note that because we need to count the center piece that we placed we just need a run length of two or more.
These days I am trying to learn java. Now I just learned how to use multiple classes and I am building the game 'brick breaker', but I don't understand why this isn't working.
public void run() {
BricksandBox world = new BricksandBox();
world.createWorld();
}
and here is the part of code from 'BricksandBox':
public void createWorld(){
buildCanvas();
int i = 0;
while(i < NBRICK_ROWS){
i++;
createARow();
setCollumNumber();
}
}
/*builds the gamecenter */
private void buildCanvas(){
GRect can = new GRect(APPLICATION_WIDTH, APPLICATION_HEIGHT);
add(can);
}
private void createARow(){
for (int i = 1; i <= NBRICKS_PER_ROW; i++){
//int y = the brickYoffset + BrickHeight+BrickSep this moves
// the brick one down.
int y = BRICK_Y_OFFSET+((BRICK_SEP+BRICK_HEIGHT)*collumNumber);
/*
* i-1 because it can't be set to zero. else there will be 11 bricks.
* so you do i-1.
*/
int x = BRICK_SEP/2+(BRICK_SEP*(i-1))+(BRICK_WIDTH*(i-1));
GRect brick = new GRect(x,y,BRICK_WIDTH,BRICK_HEIGHT);
brick.setFilled(true);
//setting the color
if(collumNumber <= 1){
brick.setColor(Color.RED);
}
else if(collumNumber <= 3){
brick.setColor(Color.ORANGE);
}
else if(collumNumber <= 5){
brick.setColor(Color.YELLOW);
}
else if(collumNumber <= 7){
brick.setColor(Color.GREEN);
}
else if(collumNumber <= 9){
brick.setColor(Color.CYAN);
}
add(brick);
}
}
private void setCollumNumber(){
//MeeBezig. create a value that counts up from 1 to 10.
//1 is the first row number. 2 is the second row number etc...
int x = BRICK_SEP/2+(BRICK_SEP*collumNumber)+(BRICK_WIDTH*collumNumber/2);
int y = BRICK_Y_OFFSET+((BRICK_HEIGHT)*collumNumber);
if(contains(x,y)){
collumNumber++;
}
}
/**
* a number that counts from one to ten
*/
private int collumNumber;'
First the part was just in the main file. But for decomposition I tried to put it in a diffrent class. but that now when I run the program, it just shows a blank canvas.
Thanks in advance!