hallo zusammen, brauch mal wieder eure hilfe!
Wir haben folgende Aufgabe bekommen:
"Spiel: Overflow
Jeder Spieler platziert abwechselnd Spielsteine auf einem Spielfeld. Wenn die Anzahl an
Spielsteinen in einer Zelle die Anzahl der senkrechten und wagerechten Nachbarzellen erreicht oder
überschreitet zählt die Zelle als übervoll. Übervolle Zellen werden auf die Nachbarzellen verteilt.
Befinden sich bereits Steine auf der überfluteten Zelle, so werden diese dem aktiven Spieler
zugerechnet. Solange noch übervolle Zellen existieren werden diese verteilt. Gewonnen hat
derjenige Spieler, der alle Steine auf dem Brett besitzt.
Ergänze die beiliegende Datei Overflow.java."
Hier die Datei Overflow.java:
Jetzt wollte ich das ersteinmal so hinbekommen, dass nur die Zellen ausgegeben werden. Man kann also noch nichts anklicken, oder so.
Doch dabei komme ich schon nicht weiter. Es kommt halt immer noch die Fehlermeldung: "incompatible types" in zeile 340.
Könnt ihr mir helfen? Vielen Dank im Vorraus,
sascha.
Wir haben folgende Aufgabe bekommen:
"Spiel: Overflow
Jeder Spieler platziert abwechselnd Spielsteine auf einem Spielfeld. Wenn die Anzahl an
Spielsteinen in einer Zelle die Anzahl der senkrechten und wagerechten Nachbarzellen erreicht oder
überschreitet zählt die Zelle als übervoll. Übervolle Zellen werden auf die Nachbarzellen verteilt.
Befinden sich bereits Steine auf der überfluteten Zelle, so werden diese dem aktiven Spieler
zugerechnet. Solange noch übervolle Zellen existieren werden diese verteilt. Gewonnen hat
derjenige Spieler, der alle Steine auf dem Brett besitzt.
Ergänze die beiliegende Datei Overflow.java."
Hier die Datei Overflow.java:
Code:
/*
Informatik A WS 04-05,
Blatt 10,
Aufgabe 3
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
Frame that plays the game "overflow".
Click with the mouse to set a stone.
Press ESC to start a new game.
Main method is included.
*/
public class Overflow extends Frame
{
/// width and height of a cell in pixel.
final int cellsize = 30;
/**
Information about player.
*/
class Player
{
/// Player name.
String name;
/// Player color.
Color color;
/**
Create player.
@param name: Name of player.
@param color: Color of player.
*/
Player(String name, Color color)
{
this.name = name;
this.color = color;
}
}
/**
The cell for the board.
*/
class Cell
{
/// Number of stones on this cell.
int stones;
/// Player who owns this cell. -1 = none, 0 = player one, 1 = player two, ...
int player;
/**
Create a empty cell.
*/
Cell()
{
stones = 0;
player = -1;
}
/**
Clear cell.
*/
void clear()
{
stones = 0;
player = -1;
}
/**
Add a stone.
@param player: Player who wants to add a stone.
@result True if the stone was added.
*/
boolean addStone(int player)
{
if (this.player != -1 && this.player != player)
return false;
this.stones++;
this.player = player;
return true;
}
/**
Add some stones and convert to player.
@param stones: Number of stones to add.
@param player: New owner of cell.
*/
void flood(int stones, int player)
{
this.stones += stones;
this.player = player;
}
void paint(Graphics gc, int x, int y)
{
if (player != -1)
{
gc.setColor(m_players[player].color);
gc.fillRect(x, y, cellsize, cellsize);
}
gc.setColor(Color.BLACK);
gc.drawRect(x, y, cellsize, cellsize);
if (stones > 0)
{
gc.setColor(Color.WHITE);
drawStringCentered(gc, Integer.toString(stones), x + cellsize / 2, y + cellsize / 2);
}
}
}
/**
Draws a centered string.
@param gc: Graphics.
@param s: String.
@param x: x position.
@param y: y position.
*/
static void drawStringCentered(Graphics gc, String s, int x, int y)
{
FontMetrics fm = gc.getFontMetrics();
gc.drawString(s, x - fm.stringWidth(s) / 2, y + fm.getAscent() / 2);
}
/// Board width in cells.
int m_xsize;
/// Board height in cells.
int m_ysize;
/// The board cells.
Cell m_cells[][];
/// The players.
Player m_players[];
/// Actual player number.
int m_player;
/// Game is running.
boolean m_playing;
/**
Create and initialise frame.
@param width: Frame width.
@param height: Frame height.
*/
public Overflow(int width,int height)
{
// Call super constructor.
super("Overflow");
// Use a white background.
setBackground(Color.white);
// Set size and show frame.
setSize(width,height);
setVisible(true);
// Calculate number of cells.
// The cells should fill the whole client area,
// but not the frame decoration.
Insets insets = getInsets();
m_xsize = (width - insets.left - insets.right) / cellsize;
m_ysize = (height - insets.top - insets.bottom) / cellsize;
// Create and initialize cells.
{
// !!!! This block is to be implemented by the student.
}
// Two players, first players starts.
m_players = new Player[2];
m_players[0] = new Player("red", Color.RED);
m_players[1] = new Player("blue", Color.BLUE);
m_player = 0;
setTitle("Overflow - Player " + m_players[m_player].name);
// Ready to play.
m_playing = true;
// A window listener that exits the program when the frame is closed.
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
// A window listener that repaints the frame when it is resized.
addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
repaint();
}
});
// A mouse listener that responds to clicks.
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (m_playing)
{
doMove((e.getX() - getInsets().left) / cellsize, (e.getY() - getInsets().top) / cellsize);
repaint();
}
}
});
// A key listener that responds to ESC.
addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
if (e.getKeyChar() == KeyEvent.VK_ESCAPE)
{
newGame();
repaint();
}
}
});
// Show cells.
repaint();
}
/**
Start a new game.
*/
void newGame()
{
for (int x = 0; x < m_xsize; x++)
{
for (int y = 0; y < m_ysize; y++)
{
m_cells[x][y].clear();
}
}
setTitle("Overflow - Player " + m_players[m_player].name);
m_playing = true;
}
/**
Make a game move.
@param x: cell x coordiante.
@param y: cell y coordinate.
*/
void doMove(int x, int y)
{
if (!addStone(x, y))
return;
updateCells();
if (checkEndOfGame())
{
m_playing = false;
setTitle("Overflow - Player " + m_players[m_player].name + " has won");
}
else
{
nextPlayer();
setTitle("Overflow - Player " + m_players[m_player].name);
}
}
/**
Give game to next player.
*/
void nextPlayer()
{
// !!!! This block is to be implemented by the student.
}
/**
Add a stone for the actual player on a cell.
The cell does not owerflow, even if its overfull.
@param x: cell x coordiante.
@param y: cell y coordinate.
@result True if the stone was set, else false.
*/
boolean addStone(int x, int y)
{
// !!!! This block is to be implemented by the student.
}
/**
Update all cells.
Overflow all overfull cells until there are no more overfull cells.
*/
void updateCells()
{
// !!!! This block is to be implemented by the student.
// !!!! Tip: Calculate the number of neighbor cells to check for overfullness.
}
/**
Check if the end of the game is reached.
This means that one player has acquired all stones.
If there are one or zero stones the game has just started.
@result True if the game has ended.
*/
boolean checkEndOfGame()
{
// !!!! This block is to be implemented by the student.
}
/**
Draws the cells.
*/
public void paint(Graphics gc)
{
// Move origin to upper left corner of client area.
gc.translate(getInsets().left, getInsets().top);
// Erase background.
Rectangle clipbounds = gc.getClipBounds();
gc.setColor(Color.WHITE);
gc.fillRect(clipbounds.x, clipbounds.y, clipbounds.width, clipbounds.height);
// Only daw cells if they exist.
if (m_cells != null)
{
// Draw cells.
gc.setFont(new Font("Monospaced", Font.BOLD, 18));
{
// !!!! This block is to be implemented by the student.
}
}
}
/**
Main method.
*/
public static void main(String args[])
{
new Overflow(600, 600);
}
}
Jetzt wollte ich das ersteinmal so hinbekommen, dass nur die Zellen ausgegeben werden. Man kann also noch nichts anklicken, oder so.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
Frame that plays the game "overflow".
Click with the mouse to set a stone.
Press ESC to start a new game.
Main method is included.
*/
public class Overflow extends Frame
{
/// width and height of a cell in pixel.
final int cellsize = 30;
/**
Information about player.
*/
class Player
{
/// Player name.
String name;
/// Player color.
Color color;
/**
Create player.
@param name: Name of player.
@param color: Color of player.
*/
Player(String name, Color color)
{
this.name = name;
this.color = color;
}
}
/**
The cell for the board.
*/
class Cell
{
/// Number of stones on this cell.
int stones;
/// Player who owns this cell. -1 = none, 0 = player one, 1 = player two, ...
int player;
/**
Create a empty cell.
*/
Cell()
{
stones = 0;
player = -1;
}
/**
Clear cell.
*/
void clear()
{
stones = 0;
player = -1;
}
/**
Add a stone.
@param player: Player who wants to add a stone.
@result True if the stone was added.
*/
boolean addStone(int player)
{
if (this.player != -1 && this.player != player)
return false;
this.stones++;
this.player = player;
return true;
}
/**
Add some stones and convert to player.
@param stones: Number of stones to add.
@param player: New owner of cell.
*/
void flood(int stones, int player)
{
this.stones += stones;
this.player = player;
}
void paint(Graphics gc, int x, int y)
{
if (player != -1)
{
gc.setColor(m_players[player].color);
gc.fillRect(x, y, cellsize, cellsize);
}
gc.setColor(Color.BLACK);
gc.drawRect(x, y, cellsize, cellsize);
if (stones > 0)
{
gc.setColor(Color.WHITE);
drawStringCentered(gc, Integer.toString(stones), x + cellsize / 2, y + cellsize / 2);
}
}
}
/**
Draws a centered string.
@param gc: Graphics.
@param s: String.
@param x: x position.
@param y: y position.
*/
static void drawStringCentered(Graphics gc, String s, int x, int y)
{
FontMetrics fm = gc.getFontMetrics();
gc.drawString(s, x - fm.stringWidth(s) / 2, y + fm.getAscent() / 2);
}
/// Board width in cells.
int m_xsize;
/// Board height in cells.
int m_ysize;
/// The board cells.
Cell m_cells[][];
/// The players.
Player m_players[];
/// Actual player number.
int m_player;
/// Game is running.
boolean m_playing;
/**
Create and initialise frame.
@param width: Frame width.
@param height: Frame height.
*/
public Overflow(int width,int height)
{
// Call super constructor.
super("Overflow");
// Use a white background.
setBackground(Color.white);
// Set size and show frame.
setSize(width,height);
setVisible(true);
// Calculate number of cells.
// The cells should fill the whole client area,
// but not the frame decoration.
Insets insets = getInsets();
m_xsize = (width - insets.left - insets.right) / cellsize;
m_ysize = (height - insets.top - insets.bottom) / cellsize;
// Create and initialize cells.
m_cells = new Cell [m_xsize][];
for (int i=0;i<m_xsize;i++)
{
m_cells[i]=new Cell [m_ysize];
}
// Two players, first players starts.
m_players = new Player[2];
m_players[0] = new Player("red", Color.RED);
m_players[1] = new Player("blue", Color.BLUE);
m_player = 0;
setTitle("Overflow - Player " + m_players[m_player].name);
// Ready to play.
m_playing = true;
// A window listener that exits the program when the frame is closed.
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
// A window listener that repaints the frame when it is resized.
addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
repaint();
}
});
// A mouse listener that responds to clicks.
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (m_playing)
{
doMove((e.getX() - getInsets().left) / cellsize, (e.getY() - getInsets().top) / cellsize);
repaint();
}
}
});
// A key listener that responds to ESC.
addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
if (e.getKeyChar() == KeyEvent.VK_ESCAPE)
{
newGame();
repaint();
}
}
});
// Show cells.
repaint();
}
/**
Start a new game.
*/
void newGame()
{
for (int x = 0; x < m_xsize; x++)
{
for (int y = 0; y < m_ysize; y++)
{
m_cells[x][y].clear();
}
}
setTitle("Overflow - Player " + m_players[m_player].name);
m_playing = true;
}
/**
Make a game move.
@param x: cell x coordiante.
@param y: cell y coordinate.
*/
void doMove(int x, int y)
{
if (!addStone(x, y))
return;
updateCells();
if (checkEndOfGame())
{
m_playing = false;
setTitle("Overflow - Player " + m_players[m_player].name + " has won");
}
else
{
nextPlayer();
setTitle("Overflow - Player " + m_players[m_player].name);
}
}
/**
Give game to next player.
*/
void nextPlayer()
{
// !!!! This block is to be implemented by the student.
}
/**
Add a stone for the actual player on a cell.
The cell does not owerflow, even if its overfull.
@param x: cell x coordiante.
@param y: cell y coordinate.
@result True if the stone was set, else false.
*/
boolean addStone(int x, int y)
{
// !!!! This block is to be implemented by the student.
}
/**
Update all cells.
Overflow all overfull cells until there are no more overfull cells.
*/
void updateCells()
{
// !!!! This block is to be implemented by the student.
// !!!! Tip: Calculate the number of neighbor cells to check for overfullness.
}
/**
Check if the end of the game is reached.
This means that one player has acquired all stones.
If there are one or zero stones the game has just started.
@result True if the game has ended.
*/
boolean checkEndOfGame()
{
// !!!! This block is to be implemented by the student.
}
/**
Draws the cells.
*/
public void paint(Graphics gc)
{
// Move origin to upper left corner of client area.
gc.translate(getInsets().left, getInsets().top);
// Erase background.
Rectangle clipbounds = gc.getClipBounds();
gc.setColor(Color.WHITE);
gc.fillRect(clipbounds.x, clipbounds.y, clipbounds.width, clipbounds.height);
// Only daw cells if they exist.
if (m_cells != null)
{
// Draw cells.
gc.setFont(new Font("Monospaced", Font.BOLD, 18));
{
gc.setColor(Color.BLACK);
int g=0;
int h;
for(int x=0;x<m_xsize*cellsize;x+=cellsize)
{
h=0;
for(int y=0;y<m_ysize*cellsize;y+=cellsize)
{
if(m_cells[g][h])
{
gc.setColor(Color.blue);
g++;
}
}
}
}
}
}
/**
Main method.
*/
public static void main(String[] args)
{
new Overflow(600, 600);
};
}
Könnt ihr mir helfen? Vielen Dank im Vorraus,
sascha.