тогаш треба во Огласи да оди поракатаАко веќе нудиш соодветен паричен надомест за решавање на задачата,
Не треба, ќе погаѓаме.Imam 5 dela sto treba da se napisat, ako treba bi postiral opis sto treba da se napravi
Implement a playing agent for the popular game Connect-4 (you can find the playing instructions on: Connect Four - Wikipedia) in its basic version - size of playing field is 7 x 6 slots, two-colored coins, two players taking turns in throwing the coins of "their" color through the opening on the top of each of the 6 columns, coins falling down the column to the first empty slot.
In the file you will find the source code (in the Java programming language), implementing the graphical user interface (GUI) for the game together with the rules of the game and the "win/lose" check at the end of the game. The compiled version of the source code is actually a complete Connect-4 game for two human players.
Samo nemozam da ja prikacam aplikacijata, ke probam Preku URL
Connect4 - Download - 4shared
Download Connect4 at 4shared free online storage servicewww.4shared.com
Part 2И може да објасниш што треба со ова задачава? Ми изгледа готова.
import javax.swing.*;
public class Connect4 {
public static void main(String[] args) {
Connect4JFrame frame = new Connect4JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Connect4JFrame extends JFrame implements ActionListener {
Panel testp = new Panel();
private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7;
private Label lblSpacer, redMC, yellowMC, totalMC;
MenuItem newMI, exitMI, redMI, yellowMI, oneP, twoP;
int[][] theArray;
boolean end=false;
boolean gameStart;
boolean playerTurn = true;
boolean bot = true;
public static final int BLANK = 0;
public static final int RED = 1;
public static final int YELLOW = 2;
public int redMoves = 0;
public int yellowMoves = 0;
public int totalMoves = (redMoves + yellowMoves);
public static final int MAXROW = 6; // 6 rows
public static final int MAXCOL = 7; // 7 columns
public static final String SPACE = " "; // 18 spaces
int activeColour = RED;
public Connect4JFrame() {
setTitle("Connect4");
MenuBar mbar = new MenuBar();
Menu fileMenu = new Menu("File");
newMI = new MenuItem("New");
newMI.addActionListener(this);
fileMenu.add(newMI);
exitMI = new MenuItem("Exit");
exitMI.addActionListener(this);
fileMenu.add(exitMI);
mbar.add(fileMenu);
Menu optMenu = new Menu("Options");
redMI = new MenuItem("Red starts");
redMI.addActionListener(this);
optMenu.add(redMI);
yellowMI = new MenuItem("Yellow starts");
yellowMI.addActionListener(this);
optMenu.add(yellowMI);
mbar.add(optMenu);
Menu playerMenu = new Menu("Players");
oneP = new MenuItem("1 Player");
oneP.addActionListener(this);
playerMenu.add(oneP);
twoP = new MenuItem("2 Players");
twoP.addActionListener(this);
playerMenu.add(twoP);
mbar.add(playerMenu);
setMenuBar(mbar);
// Build control panel.
Panel panel = new Panel();
btn1 = new Button("1");
btn1.addActionListener(this);
panel.add(btn1);
lblSpacer = new Label(SPACE);
panel.add(lblSpacer);
btn2 = new Button("2");
btn2.addActionListener(this);
panel.add(btn2);
lblSpacer = new Label(SPACE);
panel.add(lblSpacer);
btn3 = new Button("3");
btn3.addActionListener(this);
panel.add(btn3);
lblSpacer = new Label(SPACE);
panel.add(lblSpacer);
btn4 = new Button("4");
btn4.addActionListener(this);
panel.add(btn4);
lblSpacer = new Label(SPACE);
panel.add(lblSpacer);
btn5 = new Button("5");
btn5.addActionListener(this);
panel.add(btn5);
lblSpacer = new Label(SPACE);
panel.add(lblSpacer);
btn6 = new Button("6");
btn6.addActionListener(this);
panel.add(btn6);
lblSpacer = new Label(SPACE);
panel.add(lblSpacer);
btn7 = new Button("7");
btn7.addActionListener(this);
panel.add(btn7);
drawMoves();
add(testp, BorderLayout.SOUTH);
add(panel, BorderLayout.NORTH);
initialize();
// Set to a reasonable size.
setSize(1024, 768);
} // Connect4
public void initialize() {
theArray=new int[MAXROW][MAXCOL];
for (int row=0; row<MAXROW; row++)
for (int col=0; col<MAXCOL; col++)
theArray[row][col]=BLANK;
gameStart=false;
redMoves = yellowMoves = 0;
} // initialize
public void drawMoves() {
redMC = new Label("Red Player Moves: " + redMoves);
testp.add(redMC);
yellowMC = new Label("Yellow Player Moves: " + yellowMoves);
testp.add(yellowMC);
totalMC = new Label("Total Moves: " + (yellowMoves + redMoves));
testp.add(totalMC);
}
public void updateScore() {
redMC.setText("Red Player Moves: " + redMoves);
yellowMC.setText("Yellow Player Moves: " + yellowMoves);
totalMC.setText("Total Moves: " + (yellowMoves + redMoves));
}
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(110, 50, 100+100*MAXCOL, 100+100*MAXROW);
for (int row=0; row<MAXROW; row++)
for (int col=0; col<MAXCOL; col++) {
if (theArray[row][col]==BLANK) g.setColor(Color.WHITE);
if (theArray[row][col]==RED) g.setColor(Color.RED);
if (theArray[row][col]==YELLOW) g.setColor(Color.YELLOW);
g.fillOval(160+100*col, 100+100*row, 100, 100);
}
check4(g);
} // paint
public void playerCheck() {
if(playerTurn) playerTurn = false;
else playerTurn = true;
}
public void putDisk(int n) {
// put a disk on top of column n
// if game is won, do nothing
if (end) return;
gameStart=true;
int row;
n--;
for (row=0; row<MAXROW; row++)
if (theArray[row][n]>0) break;
if (row>0) {
theArray[--row][n]=activeColour;
if (activeColour==RED) {
redMoves++;
activeColour=YELLOW;
} else {
yellowMoves++;
activeColour=RED;
}
playerCheck();
updateScore();
repaint();
}
}
public void displayWinner(Graphics g, int n) {
g.setColor(Color.BLACK);
g.setFont(new Font("Courier", Font.BOLD, 100));
if (n==RED)
g.drawString("Red wins!", 250, 400);
else
g.drawString("Yellow wins!", 150, 400);
end=true;
}
public void check4(Graphics g) {
// see if there are 4 disks in a row: horizontal, vertical or diagonal
// horizontal rows
for (int row=0; row<MAXROW; row++) {
for (int col=0; col<MAXCOL-3; col++) {
int curr = theArray[row][col];
if (curr>0
&& curr == theArray[row][col+1]
&& curr == theArray[row][col+2]
&& curr == theArray[row][col+3]) {
displayWinner(g, theArray[row][col]);
}
}
}
// vertical columns
for (int col=0; col<MAXCOL; col++) {
for (int row=0; row<MAXROW-3; row++) {
int curr = theArray[row][col];
if (curr>0
&& curr == theArray[row+1][col]
&& curr == theArray[row+2][col]
&& curr == theArray[row+3][col])
displayWinner(g, theArray[row][col]);
}
}
// diagonal lower left to upper right
for (int row=0; row<MAXROW-3; row++) {
for (int col=0; col<MAXCOL-3; col++) {
int curr = theArray[row][col];
if (curr>0
&& curr == theArray[row+1][col+1]
&& curr == theArray[row+2][col+2]
&& curr == theArray[row+3][col+3])
displayWinner(g, theArray[row][col]);
}
}
// diagonal upper left to lower right
for (int row=MAXROW-1; row>=3; row--) {
for (int col=0; col<MAXCOL-3; col++) {
int curr = theArray[row][col];
if (curr>0
&& curr == theArray[row-1][col+1]
&& curr == theArray[row-2][col+2]
&& curr == theArray[row-3][col+3])
displayWinner(g, theArray[row][col]);
}
}
} // end check4
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1)
putDisk(1);
else if (e.getSource() == btn2)
putDisk(2);
else if (e.getSource() == btn3)
putDisk(3);
else if (e.getSource() == btn4)
putDisk(4);
else if (e.getSource() == btn5)
putDisk(5);
else if (e.getSource() == btn6)
putDisk(6);
else if (e.getSource() == btn7)
putDisk(7);
else if (e.getSource() == newMI) {
end=false;
initialize();
repaint();
} else if (e.getSource() == exitMI) {
System.exit(0);
} else if (e.getSource() == redMI) {
if (!gameStart) activeColour=RED;
} else if (e.getSource() == yellowMI) {
if (!gameStart) activeColour=YELLOW;
} else if (e.getSource() == oneP) {
if (!gameStart) bot = true;
} else if (e.getSource() == twoP) {
if (!gameStart) bot = false;
}
if(bot) {
int max = 7, min = 1;
int randNum = (int)(Math.random() * (max - min + 1) + min);
if(!playerTurn)
putDisk(randNum);
}
} // end ActionPerformed
} // class
Ti blagodaram, ke se obidam kolku so razbiram.Не сум баш фан на тоа целосно да пишувам код на некој друг, така ништо нема да научиш ама ајде еве.
Не е целосно решена, Part 2 и Part 3 се направени тука само (мислам дека има еден баг треба да се поправи).
Пробај 4, 5 и 6 сам да ги решиш.
Connect4.java
Java:import javax.swing.*; public class Connect4 { public static void main(String[] args) { Connect4JFrame frame = new Connect4JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Connect4JFrame.java
Java:import java.awt.*; import java.awt.event.*; import javax.swing.*; class Connect4JFrame extends JFrame implements ActionListener { Panel testp = new Panel(); private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7; private Label lblSpacer, redMC, yellowMC, totalMC; MenuItem newMI, exitMI, redMI, yellowMI, oneP, twoP; int[][] theArray; boolean end=false; boolean gameStart; boolean playerTurn = true; boolean bot = true; public static final int BLANK = 0; public static final int RED = 1; public static final int YELLOW = 2; public int redMoves = 0; public int yellowMoves = 0; public int totalMoves = (redMoves + yellowMoves); public static final int MAXROW = 6; // 6 rows public static final int MAXCOL = 7; // 7 columns public static final String SPACE = " "; // 18 spaces int activeColour = RED; public Connect4JFrame() { setTitle("Connect4"); MenuBar mbar = new MenuBar(); Menu fileMenu = new Menu("File"); newMI = new MenuItem("New"); newMI.addActionListener(this); fileMenu.add(newMI); exitMI = new MenuItem("Exit"); exitMI.addActionListener(this); fileMenu.add(exitMI); mbar.add(fileMenu); Menu optMenu = new Menu("Options"); redMI = new MenuItem("Red starts"); redMI.addActionListener(this); optMenu.add(redMI); yellowMI = new MenuItem("Yellow starts"); yellowMI.addActionListener(this); optMenu.add(yellowMI); mbar.add(optMenu); Menu playerMenu = new Menu("Players"); oneP = new MenuItem("1 Player"); oneP.addActionListener(this); playerMenu.add(oneP); twoP = new MenuItem("2 Players"); twoP.addActionListener(this); playerMenu.add(twoP); mbar.add(playerMenu); setMenuBar(mbar); // Build control panel. Panel panel = new Panel(); btn1 = new Button("1"); btn1.addActionListener(this); panel.add(btn1); lblSpacer = new Label(SPACE); panel.add(lblSpacer); btn2 = new Button("2"); btn2.addActionListener(this); panel.add(btn2); lblSpacer = new Label(SPACE); panel.add(lblSpacer); btn3 = new Button("3"); btn3.addActionListener(this); panel.add(btn3); lblSpacer = new Label(SPACE); panel.add(lblSpacer); btn4 = new Button("4"); btn4.addActionListener(this); panel.add(btn4); lblSpacer = new Label(SPACE); panel.add(lblSpacer); btn5 = new Button("5"); btn5.addActionListener(this); panel.add(btn5); lblSpacer = new Label(SPACE); panel.add(lblSpacer); btn6 = new Button("6"); btn6.addActionListener(this); panel.add(btn6); lblSpacer = new Label(SPACE); panel.add(lblSpacer); btn7 = new Button("7"); btn7.addActionListener(this); panel.add(btn7); drawMoves(); add(testp, BorderLayout.SOUTH); add(panel, BorderLayout.NORTH); initialize(); // Set to a reasonable size. setSize(1024, 768); } // Connect4 public void initialize() { theArray=new int[MAXROW][MAXCOL]; for (int row=0; row<MAXROW; row++) for (int col=0; col<MAXCOL; col++) theArray[row][col]=BLANK; gameStart=false; redMoves = yellowMoves = 0; } // initialize public void drawMoves() { redMC = new Label("Red Player Moves: " + redMoves); testp.add(redMC); yellowMC = new Label("Yellow Player Moves: " + yellowMoves); testp.add(yellowMC); totalMC = new Label("Total Moves: " + (yellowMoves + redMoves)); testp.add(totalMC); } public void updateScore() { redMC.setText("Red Player Moves: " + redMoves); yellowMC.setText("Yellow Player Moves: " + yellowMoves); totalMC.setText("Total Moves: " + (yellowMoves + redMoves)); } public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(110, 50, 100+100*MAXCOL, 100+100*MAXROW); for (int row=0; row<MAXROW; row++) for (int col=0; col<MAXCOL; col++) { if (theArray[row][col]==BLANK) g.setColor(Color.WHITE); if (theArray[row][col]==RED) g.setColor(Color.RED); if (theArray[row][col]==YELLOW) g.setColor(Color.YELLOW); g.fillOval(160+100*col, 100+100*row, 100, 100); } check4(g); } // paint public void playerCheck() { if(playerTurn) playerTurn = false; else playerTurn = true; } public void putDisk(int n) { // put a disk on top of column n // if game is won, do nothing if (end) return; gameStart=true; int row; n--; for (row=0; row<MAXROW; row++) if (theArray[row][n]>0) break; if (row>0) { theArray[--row][n]=activeColour; if (activeColour==RED) { redMoves++; activeColour=YELLOW; } else { yellowMoves++; activeColour=RED; } playerCheck(); updateScore(); repaint(); } } public void displayWinner(Graphics g, int n) { g.setColor(Color.BLACK); g.setFont(new Font("Courier", Font.BOLD, 100)); if (n==RED) g.drawString("Red wins!", 250, 400); else g.drawString("Yellow wins!", 150, 400); end=true; } public void check4(Graphics g) { // see if there are 4 disks in a row: horizontal, vertical or diagonal // horizontal rows for (int row=0; row<MAXROW; row++) { for (int col=0; col<MAXCOL-3; col++) { int curr = theArray[row][col]; if (curr>0 && curr == theArray[row][col+1] && curr == theArray[row][col+2] && curr == theArray[row][col+3]) { displayWinner(g, theArray[row][col]); } } } // vertical columns for (int col=0; col<MAXCOL; col++) { for (int row=0; row<MAXROW-3; row++) { int curr = theArray[row][col]; if (curr>0 && curr == theArray[row+1][col] && curr == theArray[row+2][col] && curr == theArray[row+3][col]) displayWinner(g, theArray[row][col]); } } // diagonal lower left to upper right for (int row=0; row<MAXROW-3; row++) { for (int col=0; col<MAXCOL-3; col++) { int curr = theArray[row][col]; if (curr>0 && curr == theArray[row+1][col+1] && curr == theArray[row+2][col+2] && curr == theArray[row+3][col+3]) displayWinner(g, theArray[row][col]); } } // diagonal upper left to lower right for (int row=MAXROW-1; row>=3; row--) { for (int col=0; col<MAXCOL-3; col++) { int curr = theArray[row][col]; if (curr>0 && curr == theArray[row-1][col+1] && curr == theArray[row-2][col+2] && curr == theArray[row-3][col+3]) displayWinner(g, theArray[row][col]); } } } // end check4 public void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) putDisk(1); else if (e.getSource() == btn2) putDisk(2); else if (e.getSource() == btn3) putDisk(3); else if (e.getSource() == btn4) putDisk(4); else if (e.getSource() == btn5) putDisk(5); else if (e.getSource() == btn6) putDisk(6); else if (e.getSource() == btn7) putDisk(7); else if (e.getSource() == newMI) { end=false; initialize(); repaint(); } else if (e.getSource() == exitMI) { System.exit(0); } else if (e.getSource() == redMI) { if (!gameStart) activeColour=RED; } else if (e.getSource() == yellowMI) { if (!gameStart) activeColour=YELLOW; } else if (e.getSource() == oneP) { if (!gameStart) bot = true; } else if (e.getSource() == twoP) { if (!gameStart) bot = false; } if(bot) { int max = 7, min = 1; int randNum = (int)(Math.random() * (max - min + 1) + min); if(!playerTurn) putDisk(randNum); } } // end ActionPerformed } // class