Reputation: 99
I am creating a boardgame in java, and I'm trying to write a method that flags a chosen object (object represents a Tile on the board) by the user during the game. The method is within a class that sets a single Tile's value and position on the board.
I think using enum types would be a good idea but I am not sure exactly how to implement this. Within my class I have methods that get a Tile's position(row, column) on the grid, and the letter of which it represents.
public class Tile {
private final String letter; //holds the letter value of the tile
private final int row; //holds tile row index
private final int column;
public Tile(String l, int r, int c) {
this.letter = l;
this.row = r;
this.column = c;
}
//setter&getter methods
public String toString() {
return this.getLetter()+" "+ this.getRow() +
"," + this.getColumn();
}
So within this class as well, I want to write a method that flags whether or not a tile object is chosen... I was thinking that if the toString method returns a statement, then that can be used to show that the tile has been chosen. Or... how should I go about this. This is what I have so far:
public enum Status {CHOSEN, NOTCHOSEN};
public static void tileStatus(Status stat){
switch(stat) {
case CHOSEN: //something
break;
case NOTCHOSEN: //something
break;
}
}
Upvotes: 1
Views: 606
Reputation: 833
Make enum
as member variable of class
and method to the enum
.
like this as bellow:-
package com.robo.lab;
public class Tile {
private final String letter; // holds the letter value of the tile
private final int row; // holds tile row index
private final int column;
private Status status;
public Tile(String l, int r, int c,Status status) {
this.letter = l;
this.row = r;
this.column = c;
this.status=status;
}
// setter&getter methods
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public String toString() {
return this.getLetter() + " " + this.getRow() + "," + this.getColumn()+","+this.getStatus();
}
public String getLetter() {
return letter;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}
package com.robo.lab;
public enum Status {
CHOSEN, NOTCHOSEN;
public static void tileStatus(Status stat) {
switch (stat) {
case CHOSEN: // something
break;
case NOTCHOSEN: // something
break;
}
}
}
package com.robo.lab;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Tile obj1= new Tile("AUser", 1, 1,Status.CHOSEN);
System.out.println(obj1.toString());
Tile obj2= new Tile("BUser", 1, 1,Status.NOTCHOSEN);
System.out.println(obj2.toString());
}
}
Upvotes: 0
Reputation: 4997
Adding a boolean to the Tile may help you with the state. Since there are only two possible states (chosen, not chosen), a boolean may make more sense. Also don't add getters and setters by default. Only when you need them. Refer to "tell don't ask principle"
public class Tile {
private final String letter; //holds the letter value of the tile
private final int row; //holds tile row index
private final int column;
private boolean isTileFlagged;
public Tile(String l, int r, int c) {
this.letter = l;
this.row = r;
this.column = c;
isTileFlagged = false; // May be false to being with
}
// add getters/setters only when necessary
public void toggleFlaggedState(){
isTileFlagged = !isTileFlagged;
}
public String toString() {
return this.getLetter()+" "+ this.getRow() +
"," + this.getColumn();
}
// add hashcode, equals if necessary
Also, if the enum is necessary, it could be an inner state of Tile class, as its independent existence may not make sense.
Upvotes: 1
Reputation: 3699
you can declare that enum is instance member of Tile
class
public class Tile {
private final String letter; //holds the letter value of the tile
private final int row; //holds tile row index
private final int column;
private Status flag; // use getter and setter to set flag on using Status enum
public Tile(String l, int r, int c) {
this.letter = l;
this.row = r;
this.column = c;
}
//setter&getter methods
public String toString() {
return this.getLetter()+" "+ this.getRow() +
"," + this.getColumn();
}
Upvotes: 1