////Starter
import javax.swing.JFrame;
public class Starter extends JFrame {
public Starter()
{
add(new Board());
setTitle("Board");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new Starter();
}
}
////Board
//BOARD CLASS
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
public class Board extends JPanel implements Runnable, MouseListener{
boolean ingame = true;
private Dimension d;
int BOARD_WIDTH=500;
int BOARD_HEIGHT=500;
int x = 0;
BufferedImage img;
String message = "Welcome to Connect Four, Click to Start";
private Thread animator;
int counter = 0;
int xCell =0;
int yCell =0;
boolean gameOver = false;
int[][] cells = new int[7][6];
public Board(){
addKeyListener(new TAdapter());
addMouseListener(this);
setFocusable(true);
d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
setBackground(Color.green);
/*
* try {
* img = ImageIO.read(this.getClass().getResource("mount.jpg"));
* } catch (IOException e) {
* System.out.println("Image could not be read");
* // System.exit(1);
* }
*/
if (animator == null || !ingame) {
animator = new Thread(this);
animator.start();
}
setDoubleBuffered(true);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.blue);
g.fillRect(0, 0, d.width, d.height);
g.setColor(Color.white);
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = this.getFontMetrics(small);
g.setFont(small);
if(ingame == true){
for(int i = 0; i < 7; i ++){
for(int j = 0; j<6; j++){
if(cells[i][j] == 1){
g.setColor(Color.yellow);
}
if(cells[i][j] == 2){
g.setColor(Color.red);
}
g.fillOval(i * 60 + 47,j * 60 + 42,40,40);
g.setColor(Color.white);
}
}
}
if(gameOver == true){
g.drawString(message, 100, 250);
}
win();
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void win(){
for(int i = 0; i < 7; i ++){
for(int q = 0; q<6; q++){
try{
if(cells[0][q] == 2 && cells[0][q-1] == 2 &&
cells[0][q-2] == 2 && cells[0][q-3] == 2){
message = "Player 2 wins, click to play again";
gameOver = true;
if(cells[i][q] == 2){
if(cells[i][q-1] == 2 && cells[i][q-2] == 2 && cells[i][q-3] == 2){
message = "Player 2 wins, click to play again";
gameOver = true;
}
if(cells[i-1][q-1] == 2 && cells[i-2][q-2] == 2 && cells[i-3][q-3] == 2){
message = "Player 2 wins, click to play again";
gameOver = true;
}
if(cells[i+1][q-1] == 2 && cells[i+2][q-2] == 2 && cells[i+3][q-3] == 2){
message = "Player 2 wins, click to play again";
gameOver = true;
}
if(cells[i-1][q+1] == 2 && cells[i-2][q+2] == 2 && cells[i-3][q+3] == 2){
message = "Player 2 wins, click to play again";
gameOver = true;
}
}
if(cells[i][q] == 2){
if(cells[i-1][q-1] == 1 && cells[i-2][q-2] == 1 && cells[i-3][q-3] == 1){
message = "Player 1 wins, click to play again";
gameOver = true;
}
if(cells[i+1][q-1] == 1 && cells[i+2][q-2] == 1 && cells[i+3][q-3] == 1){
message = "Player 1 wins, click to play again";
gameOver = true;
}
if(cells[i-1][q+1] == 1 && cells[i-2][q+2] == 1 && cells[i-3][q+3] == 1){
message = "Player 1 wins, click to play again";
gameOver = true;
}
}
}
if(cells[0][q] == 1 && cells[0][q-1] == 1 &&
cells[0][q-2] == 1 && cells[0][q-3] == 1){
message = "Player 1 wins, click to play again";
gameOver = true;
}
}catch(Exception e){
}
if(cells[0][q] == 1){
if(cells[i-1][q] == 1 && cells[i-2][q] == 1 && cells[i-3][q] == 1){
message = "Player 1 wins, click to play again";
gameOver = true;
}
if(cells[i-1][q] == 2 && cells[i-2][q] == 2 && cells[i-3][q] == 2){
message = "Player 2 wins, click to play again";
gameOver = true;
}
if(cells[i][q-1] == 1){
if( cells[i][q-2] == 1 && cells[i][q-3] == 1){
message = "Player 1 wins, click to play again";
gameOver = true;
}
}
}
}
}
if(gameOver == true){
ingame = false;
}
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
}
public void keyPressed(KeyEvent e) {
//System.out.println( e.getKeyCode());
// message = "Key Pressed: " + e.getKeyCode();
int key = e.getKeyCode();
if(key==39){
}
}
}
public void mousePressed(MouseEvent e) {
counter ++;
int x = e.getX();
int y = e.getY();
xCell = ((x-47)/60);
yCell = ((y-43)/60);
boolean before = false;
for(int i=5; i>=0; i--){
if(cells[xCell][i] == 0 && gameOver == false){
if(counter%2== 0)
cells[xCell][i] = 2;
else
cells[xCell][i] = 1;
if(before)
cells[xCell][i] = 0;
before = true;
}
}
if(gameOver){
gameOver = false;
ingame = true;
cells = new int [7][6];
message = "";
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
int animationDelay = 500;
long time =
System.currentTimeMillis();
while (true) {
// spriteManager.update();
repaint();
try {
time += animationDelay;
Thread.sleep(Math.max(0,time -
System.currentTimeMillis()));
}catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.