| |
Mancala.java
class Players implements PlayerInterface{
private String name;
// Constructor
Players(String name){
setName(name);
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public int chooseBin(int[] tab){
return 1;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.Box;
/**
*/
class Mancala extends JFrame{
public ExecutivScreen EXSCREEN;
public Board BOARD;
public ExecutivBoard EXBOARD;
public Game GAME;
// Constructor
Mancala(){
this.setSize(600,600);
this.setLocation(0,0);
this.setTitle("Mancala");
this.GAME = new Game(this);
EXBOARD = new ExecutivBoard(GAME);
BOARD = new Board(GAME, BoxLayout.LINE_AXIS);
EXSCREEN = new ExecutivScreen(GAME, "sc",10,10);
Box panel = new Box(BoxLayout.PAGE_AXIS );
panel.add(EXBOARD);
panel.add(BOARD);
panel.add(EXSCREEN);
this.getContentPane().add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String args[]) {
Mancala m = new Mancala();
}
public void displayDialog(String text){
String val = this.EXSCREEN.getText();
this.EXSCREEN.setText(text+ "n" + val );
}
}
/**
*/
class ExecutivBoard extends JPanel {
// Constructor
ExecutivBoard(Game GAME){
ExecutivButton b1 = new ExecutivButton(GAME,"save","Save",200,150);
this.add(b1);
ExecutivButton b2 = new ExecutivButton(GAME,"open","Open",200,150);
this.add(b2);
ExecutivButton b3 = new ExecutivButton(GAME,"newgame","New game",200,150);
this.add(b3);
ExecutivButton b4 = new ExecutivButton(GAME,"exit","Exit",200,150);
this.add(b4);
}
}
/**
*/
class ExecutivButton extends JButton implements ActionListener{
public Game GAME;
// Constructor
ExecutivButton(Game GAME, String name, String label, int width, int height){
this.setName(name);
this.setText(label);
this.setSize(width, height);
this.GAME=GAME;
this.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if (source instanceof JButton){
JButton b =(JButton)source;
if (b.getName().equalsIgnoreCase("save")){
this.GAME.saveGame();
} else if (b.getName().equalsIgnoreCase("open")){
this.GAME.restoreGame();
} else if (b.getName().equalsIgnoreCase("exit")){
this.GAME.exitGame();
} else if (b.getName().equalsIgnoreCase("newgame")){
this.GAME.newGame();
this.GAME.getIHM().BOARD.updater();
}
}
}
}
/**
*/
class ExecutivScreen extends JTextArea{
public Game GAME;
// Constructor
ExecutivScreen(Game GAME, String name, int rows, int columns){
this.setName(name);
this.setRows(rows);
this.setColumns(columns);
this.setAutoscrolls(true);
this.setCaretColor(Color.blue);
this.GAME=GAME;
}
}
/**
*/
class Board extends Box {
public Game GAME;
public Bowl[] board;
// Constructor
Board(Game GAME, int axis){
super(axis);
this.GAME=GAME;
board = new Bowl[14];
KalahasBowl kA = new KalahasBowl(GAME,"DOWN", String.valueOf(6),String.valueOf(GAME.getPlate().getBins(6).getNbrStone()),200,100);
KalahasBowl kB = new KalahasBowl(GAME, "UP", String.valueOf(13),String.valueOf(GAME.getPlate().getBins(13).getNbrStone()),200,100);
board[6]=kA;
board[13]=kB;
Box b2 = new Box(BoxLayout.Y_AXIS);
Box b3 = new Box(BoxLayout.X_AXIS);
Box b4 = new Box(BoxLayout.X_AXIS);
for (int i=0;i<6;i++){
PlayerBowl b = new PlayerBowl(GAME,"DOWN", String.valueOf(i),String.valueOf(GAME.getPlate().getBins(i).getNbrStone()),100,100);
b4.add(b);
board[i]=b;
}
for (int i=12;i>6;i--){
PlayerBowl b = new PlayerBowl(GAME,"UP", String.valueOf(i),String.valueOf(GAME.getPlate().getBins(i).getNbrStone()),100,100);
b3.add(b);
board[i]=b;
}
this.add(kB);
b2.add(b3);
b2.add(b4);
this.add(b2);
this.add(kA);
}
public void updater(){
for (int i=0;i<14;i++){
Bowl mb = (Bowl)this.board[i];
int index = Integer.parseInt(mb.getName());
String val = String.valueOf(this.GAME.getPlate().getBins(index).getNbrStone());
mb.setText(val);
}
}
}
/**
*/
abstract class Bowl extends JButton implements ActionListener{
public Game GAME;
public String owner;
// Constructor
Bowl(Game GAME, String owner, String name,String label, int height, int width){
this.setName(name);
this.setText(label);
this.owner=owner;
this.setSize(width, height);
this.GAME = GAME;
this.addActionListener(this);
}
//
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if (source instanceof PlayerBowl){
PlayerBowl b =(PlayerBowl)source;
if(GAME.getActivPlayer().getName().equalsIgnoreCase(b.owner)){
int index = Integer.parseInt(b.getName());
GAME.distribution(index,GAME.getActivPlayer());
GAME.getIHM().BOARD.updater();
} else {
this.GAME.getIHM().displayDialog("Wrong Player");
}
} else if (source instanceof KalahasBowl) {
this.GAME.getIHM().displayDialog("Illegal Action");
}
}
}
/**
*/
class PlayerBowl extends Bowl{
// Constructor
PlayerBowl(Game GAME,String owner, String name,String label, int height, int width){
super( GAME, owner, name, label, height, width);
this.setBackground(Color.green);
}
}
/**
*/
class KalahasBowl extends Bowl{
// Constructor
KalahasBowl(Game GAME,String owner, String name, String label, int height, int width){
super( GAME, owner, name, label, height, width);
this.setBackground(Color.red);
}
}
/**
*/
import java.util.*;
class Game {
private PlayerInterface playerDown;
private PlayerInterface playerUp;
private PlayerInterface playerUnknown;
public Referee referee;
public Plate plate;
public Mancala IHM;
public PlayerInterface activPlayer;
// Constructor
Game(Mancala ihm){
this.IHM=ihm;
this.playerUnknown = new Players("UNKNOWN");
this.playerDown = new Players("DOWN");
this.playerUp = new Players("UP");
this.referee = new Referee(playerDown,playerUp);
referee.setGame(this);
setActivPlayer(playerUnknown);
this.initialisation();
}
public void initialisation(){
this.plate = new Plate(4);
referee.startPlay();
}
public void newGame(){
this.getIHM().displayDialog("+++++++++");
this.getIHM().displayDialog("NEW GAME");
this.getIHM().displayDialog("+++++++++");
this.initialisation();
}
public Plate getPlate() {return this.plate;}
public void setPlate(Plate plate) {this.plate=plate;}
public Mancala getIHM(){return this.IHM;}
public void setActivPlayer(PlayerInterface player){this.activPlayer = player;}
public PlayerInterface getActivPlayer(){return this.activPlayer;}
public void distribution(int index, PlayerInterface player) {
int nbrStoneInitial = this.getPlate().getBins(index).getNbrStone();
this.getPlate().getBins(index).setNbrStone(0);
if (nbrStoneInitial == 0){
this.getIHM().displayDialog("No stone, try again");
return;
}
int j = index;
for (int i=nbrStoneInitial; i>0;i--){
j++;
if( (j == 6) && (player.getName().equalsIgnoreCase("UP")) ){j++;}
if( (j == 13) && (player.getName().equalsIgnoreCase("DOWN")) ){j=0;}
if (j > 13){j=0;}
int n = this.getPlate().getBins(j).getNbrStone();
this.getPlate().getBins(j).setNbrStone(n+1);
if ((i == 1)&&(n == 0)&&(j!=6)&&(j!=13)) {
capture(player, j);
}
}
if( (j == 13) && (player.getName().equalsIgnoreCase("UP")) ){secondTurn();}
else if( (j == 6) && (player.getName().equalsIgnoreCase("DOWN")) ){secondTurn();}
else { referee.nextPlay(player); }
}
public void capture(PlayerInterface player, int index) {
int kalahasIndex=0;
if ( player.getName().equalsIgnoreCase("DOWN")){
if (index > 5){ return;}
kalahasIndex=6;
} else if ( player.getName().equalsIgnoreCase("UP")){
if (index < 6){ return;}
kalahasIndex=13;
}
int nbrStonePlayer = this.getPlate().getBins(index).getNbrStone();
int nbrStoneCaptured = this.getPlate().getBins(12-index).getNbrStone();
if (nbrStoneCaptured == 0) { return; }
this.getPlate().getBins(index).setNbrStone(0);
this.getPlate().getBins(12-index).setNbrStone(0);
int nbrStoneWon=nbrStoneCaptured+nbrStonePlayer;
this.getIHM().displayDialog("CAPTURE: Bol " + String.valueOf(index) +
" capture Bol " + String.valueOf(12-index) +
" ajoute a " + String.valueOf(kalahasIndex) +
" le total de" + String.valueOf(nbrStoneWon));
int nbrStone = this.getPlate().getBins(kalahasIndex).getNbrStone();
this.getPlate().getBins(kalahasIndex).setNbrStone(nbrStone+nbrStoneWon);
}
public void secondTurn() {
this.getIHM().displayDialog("One more time");
}
public void gameOver() {
this.getIHM().displayDialog("+++++++++");
this.getIHM().displayDialog("GAME OVER");
this.getIHM().displayDialog("+++++++++");
Kalahas kDOWN = (Kalahas)this.getPlate().getBins(6);
Kalahas kUP = (Kalahas)this.getPlate().getBins(13);
if (kUP.getNbrStone() > kDOWN.getNbrStone()){
this.getIHM().displayDialog("UP is the winner with a score of "+
String.valueOf(kUP.getNbrStone()) + " for "+
String.valueOf(kDOWN.getNbrStone()) );
} else {
this.getIHM().displayDialog("DOWN is the winner with a score of "+
String.valueOf(kDOWN.getNbrStone()) + " for "+
String.valueOf(kUP.getNbrStone()) );
}
this.getIHM().displayDialog("+++++++++");
}
public void saveGame() {
this.getIHM().displayDialog("SAVE GAME");
}
public void restoreGame() {
this.getIHM().displayDialog("RESTORE GAME");
}
public void exitGame(){
this.getIHM().displayDialog("EXIT GAME");
System.exit(0);
}
}
/**
*/
class Referee {
public PlayerInterface playerB;
public PlayerInterface playerA;
public Game game;
// Constructor
Referee(PlayerInterface playerA, PlayerInterface playerB){
this.playerA=playerA;
this.playerB=playerB;
}
public void startPlay(){
game.setActivPlayer(this.playerA);
}
public void nextPlay(PlayerInterface player){
if (!winner()){
String actualPlayer = game.getActivPlayer().getName();
if ( actualPlayer.equalsIgnoreCase(playerA.getName())){
game.getIHM().displayDialog("It's "+playerB.getName()+" turn!");
game.setActivPlayer(playerB);
} else if ( actualPlayer.equalsIgnoreCase(playerB.getName())){
game.getIHM().displayDialog("It's "+playerA.getName()+" turn!");
game.setActivPlayer(playerA);
}
}
}
public Game getGame() {return this.game;}
public void setGame(Game game) {this.game=game;}
public boolean winner() {
Kalahas kalahasDOWN = (Kalahas)game.getPlate().getBins(6);
Kalahas kalahasUP = (Kalahas)game.getPlate().getBins(13);
int totalDOWN=0;
int totalUP=0;
if (kalahasDOWN.isItFull()){
game.gameOver();
return true;
} else if (kalahasUP.isItFull()){
game.gameOver();
return true;
} else {
for ( int i=0;i<6;i++){
totalDOWN = totalDOWN + game.getPlate().getBins(i).getNbrStone();
}
for ( int i=7;i<13;i++){
totalUP = totalUP + game.getPlate().getBins(i).getNbrStone();
}
if ((totalDOWN == 0) || (totalUP == 0)) {
game.gameOver();
return true;
} else {
return false;
}
}
}
}
/**
*/
class Bin extends Bins {
// Constructor
Bin(int position, int nbrStone){
super( position, nbrStone);
}
public boolean isItEmpty(){
if (this.getNbrStone() == 0){
return true;
} else {
return false;
}
}
}
/**
*/
abstract class Bins {
private int nbrStone;
private int position;
// Constructor
Bins(int position, int nbrStone){
this.setNbrStone(nbrStone);
this.setPosition(position);
}
public void setNbrStone(int nbrStone) {this.nbrStone=nbrStone;}
public void setPosition(int position) {this.position=position;}
public int getNbrStone() {return this.nbrStone;}
public int getPosition() {return this.position;}
}
/**
*/
class Kalahas extends Bins {
// Constructor
Kalahas(int position, int nbrStone){
super( position, nbrStone);
}
public boolean isItFull(){
if (this.getNbrStone() > 25) {
return true;
} else {
return false;
}
}
}
/**
*/
class Plate {
public Bins[] plate;
// Constructor
Plate(int nbrStone){
plate=new Bins[14];
for (int i=0;i<6;i++){
this.setBins(i,new Bin(i,nbrStone));
}
for (int i=12;i>6;i--){
this.setBins(i,new Bin(i,nbrStone));
}
this.setBins(6,new Kalahas(6,0));
this.setBins(13,new Kalahas(13,0));
}
public void setBins(int position, Bins bins) {this.plate[position]=bins;}
public Bins getBins(int position) {return this.plate[position];}
//public int[] getTabIndex() {}
}
/**
*/
interface PlayerInterface {
public void setName(String name);
public String getName();
public int chooseBin(int[] tab);
}
1
|