Welcome to Dream.In.Code
Become a Java Expert!

Join 149,935 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,455 people online right now. Registration is fast and FREE... Join Now!




Works on house computer but not school computer

 
Reply to this topicStart new topic

Works on house computer but not school computer, problems with my connect four program working at my house but not at s

marathdamane91
16 Jun, 2008 - 08:40 AM
Post #1

New D.I.C Head
*

Joined: 1 Apr, 2008
Posts: 9

First off this is school help but i'm not looking for help with writing the program only wth why it works at my house but not at my school. Heres my code and when im at my house it will run fine but when im at school i can move my mouse once before the whole game freezes up. And then at my house it flasshes a lot is that because i need to either back buffer ordouble buffer it?
CODE

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.applet.*;

public class ConnectFour extends Applet implements Runnable, MouseListener, MouseMotionListener
{
    Random generator = new Random();
    Thread mythread;
    public boolean game = true;
    public int thecounter, startx, starty, mx, my, thex, they, finalx, finaly, entermouseX, entermouseY, exitmouseX, exitmouseY, drawX = -100, turn = 0, speed = 10, timecounter = 0;
    public int[][] board = new int[7][7];
    boolean redWins = false, blackWins = false;        
    public void init()
    {
        setBackground (Color.white);
        setSize(1000, 900);
        addMouseListener(this);
        addMouseMotionListener(this);
    }    
    public void start()
    {
        mythread = new Thread(this);
        mythread.start();
    }
    public void run()
    {
        while(game == true)
        {    
            showStatus("" + thex + they);
            repaint();
            try
            {
                Thread.sleep(speed);
            }
            catch (InterruptedException e) {; }
        }
    }
    
    public void mouseClicked (MouseEvent e){  }
    public void mouseReleased (MouseEvent e)
    {
        timecounter = 0;
        finalx = e.getX();
        finaly = e.getY();
        if ((finalx>=85)&&(finalx<=187))
        {
            int counter = 6;
            while ((board[0][counter] > 0) && (counter >= 1))
                counter--;
            if (counter >= 1)
            {
                board[0][counter] = turn%2+1;
                turn++;
            }
        }
        if ((finalx>=187)&&(finalx<287))
        {
            int counter = 6;
            while ((board[1][counter] > 0) && (counter >=1))
                counter--;
            if (counter >= 1)
            {
                board[1][counter] = turn%2+1;
                turn++;
            }
        }
        if ((finalx>=287)&&(finalx<387))
        {
            int counter = 6;
            while ((board[2][counter] > 0) && (counter >=1))
                counter--;
            if (counter >= 1)
            {
                board[2][counter] = turn%2+1;
                turn++;
            }
        }
        if ((finalx>=387)&&(finalx<487))
        {
            int counter = 6;
            while ((board[3][counter] > 0) && (counter >=1))
                counter--;
            if (counter >=1)
            {
                board[3][counter] = turn%2+1;
                turn++;
            }
        }
        if ((finalx>=487)&&(finalx<587))
        {
            int counter = 6;
            while ((board[4][counter] > 0) && (counter >=1))
                counter--;
            if (counter >=1)
            {
                board[4][counter] = turn%2+1;
                turn++;
            }
        }
        if ((finalx>=587)&&(finalx<687))
        {
            int counter = 6;
            while ((board[5][counter] > 0) && (counter >=1))
                counter--;
            if (counter >=1)
            {
                board[5][counter] = turn%2+1;
                turn++;
            }
        }
        if ((finalx>=687)&&(finalx<787))
        {
            int counter = 6;
            while ((board[6][counter] > 0) && (counter >=1))
                counter--;
            if (counter >=1)
            {
                board[6][counter] = turn%2+1;
                turn++;
            }
        }
        
        int counterRed = 0, counterBlack = 0;
        for (int x = 0; x < 7; x++)
        {
            counterRed = 0;
            counterBlack = 0;
            for (int y = 0; y < 6; y++)
            {
                if (board[x][y] == 1)
                {
                    counterBlack++;
                    counterRed = 0;
                    if (counterBlack == 4)
                        blackWins = true;
                }
                if (board[x][y] == 2)
                {
                    counterRed++;
                    counterBlack = 0;
                    if (counterRed == 4)
                        redWins = true;
                }
                if (board[x][y] == 0)
                {
                    counterRed = 0;
                    counterBlack = 0;
                }
            }
        }
        for (int y = 0; y < 6; y++)
        {
            counterRed = 0;
            counterBlack = 0;
            for (int x = 0; x < 7; x++)
            {
                if (board[x][y] == 1)
                {
                    counterBlack++;
                    counterRed = 0;
                    if (counterBlack == 4)
                        blackWins = true;
                }
                if (board[x][y] == 2)
                {
                    counterRed++;
                    counterBlack = 0;
                    if (counterRed == 4)
                        redWins = true;
                }
                if (board[x][y] == 0)
                {
                    counterRed = 0;
                    counterBlack = 0;
                }
            }
        }
        for (int x = 0; x < 4; x++)
        {
            counterRed = 0;
            counterBlack = 0;
            for (int y = 0; y < 3; y++)
            {
                counterRed = 0;
                counterBlack = 0;
                for (int z = 0; z < 4; z++)
                {
                    if (board[x+z][y+z] == 1)
                    {
                        counterBlack++;
                        counterRed =  0;
                        if (counterBlack == 4)
                            blackWins = true;
                    }
                    if (board[x+z][y+z] == 2)
                    {
                        counterRed++;
                        counterBlack = 0;
                        if (counterRed == 4)
                            redWins = true;
                    }
                    if (board[x+z][y+z] == 0)
                    {
                        counterRed = 0;
                        counterBlack = 0;
                    }    
                }
            }
        }
        for (int x = 0; x < 4; x++)
        {
            counterRed = 0;
            counterBlack = 0;
            for (int y = 3; y < 6; y++)
            {
                counterRed = 0;
                counterBlack = 0;
                for (int z = 0; z < 4; z++)
                {
                    if (board[x+z][y-z] == 1)
                    {
                        counterBlack++;
                        counterRed =  0;
                        if (counterBlack == 4)
                            blackWins = true;
                    }
                    if (board[x+z][y-z] == 2)
                    {
                        counterRed++;
                        counterBlack = 0;
                        if (counterRed == 4)
                            redWins = true;
                    }
                    if (board[x+z][y-z] == 0)
                    {
                        counterRed = 0;
                        counterBlack = 0;
                    }    
                }                
            }
        }
    }
    public void mouseEntered (MouseEvent e)
    {
        entermouseX = e.getX();
        entermouseY = e.getY();
    }
    public void mouseExited (MouseEvent e)
    {
        exitmouseX = e.getX();
        exitmouseY = e.getY();
    }
    
    public void mousePressed (MouseEvent e)
    {
        startx = e.getX();
        starty = e.getY();
        if ((startx>=87)&&(startx<=187))
            drawX=100;
        if ((startx>=187)&&(startx<287))
            drawX=200;
        if ((startx>=287)&&(startx<387))
            drawX=300;
        if ((startx>=387)&&(startx<487))
            drawX=400;
        if ((startx>=487)&&(startx<587))
            drawX=500;
        if ((startx>=587)&&(startx<687))
            drawX=600;
        if ((startx>=687)&&(startx<787))
            drawX=700;          
    }
    
    public void mouseDragged (MouseEvent e)
    {
        mx = e.getX();
        my = e.getY();
        if ((mx>=87)&&(mx<=187))
            drawX=100;
        if ((mx>=187)&&(mx<287))
            drawX=200;
        if ((mx>=287)&&(mx<387))
            drawX=300;
        if ((mx>=387)&&(mx<487))
            drawX=400;
        if ((mx>=487)&&(mx<587))
            drawX=500;
        if ((mx>=587)&&(mx<687))
            drawX=600;
        if ((mx>=687)&&(mx<787))
            drawX=700;        
    }
    
    public void mouseMoved (MouseEvent e)
    {
        thex = e.getX();
        they = e.getY();
        if ((thex>=85)&&(thex<=187))
            drawX=100;
        if ((thex>=187)&&(thex<287))
            drawX=200;
        if ((thex>=287)&&(thex<387))
            drawX=300;
        if ((thex>=387)&&(thex<487))
            drawX=400;
        if ((thex>=487)&&(thex<587))
            drawX=500;
        if ((thex>=587)&&(thex<687))
            drawX=600;
        if ((thex>=687)&&(thex<787))
            drawX=700;        
    }


    public void paint (Graphics page)
    {
        super.paint(page);
        Graphics2D page2 = (Graphics2D)page;
        setBackground(Color.yellow);
        
        for (int x = 1; x < 8; x++)
        {
            for (int y = 0; y < 6; y++)
            {
                page.setColor(Color.white);
                page.fillOval(x*100, 100*y + 100, 75, 75);
                page.setColor(Color.black);
                page.drawOval(x*100, 100*y + 100, 75, 75);
            }
        }
        for (int x = 1; x <= 6; x++)
        {
            page.setColor(new Color (112, 128, 144));
            page.drawLine(x*100+87, 92, x*100+87, 700);
        }
        page.setColor(new Color (112, 128, 144));
        for (int y = 1; y <= 5; y++)
            page.drawLine(75, 100*y+87, 920,100*y+87);
            
          
            
        
        page.setColor(Color.white);
        page.fillRect(85, 0, 705, 90);
        page.fillRect(0, 0, 85, 900);
        page.fillRect(787, 0, 1000, 900);
        page.fillRect(75,690,1000,900);
        page.setColor(Color.black);
        page.drawRect(85, 90, 702, 600);
        
      
      
        if (turn%2 == 0)
            page.setColor(Color.black);
        if (turn%2 == 1)
            page.setColor(Color.red);
        if ((redWins == false) && (blackWins == false))
            page.fillOval(drawX, 10, 75, 75);
        
        for (int x = 1; x <= 7; x++)
        {
            for (int y = 6; y >= 1; y--)
            {
                if (board[x][y] == 1)
                    page.setColor(Color.black);
                if (board[x][y] == 2)
                    page.setColor(Color.red);
                if (board[x][y] > 0)
                    page.fillOval(100*x,100*y, 75, 75);
            }
        }
        if (redWins == true)
            page.drawString("RED WINS", 400, 30);
        if (blackWins == true)
            page.drawString("BLACK WINS", 400, 30);  
        thecounter = 0;
        for (int x = 0; x < 7; x++)
            for (int y = 0; y < 6; y++)
                if (board[x][y] == 0)
                    thecounter++;
        if (thecounter == 0)
            page.drawString("It's a tie", 400, 30);
    }
}

User is offlineProfile CardPM
+Quote Post

jjsaw5
RE: Works On House Computer But Not School Computer
16 Jun, 2008 - 09:02 AM
Post #2

I must break you
Group Icon

Joined: 4 Jan, 2008
Posts: 1,632



Thanked: 8 times
Dream Kudos: 125
My Contributions
if its a game, maybe your school has stuff like this blocked and thats why it's freezing
User is offlineProfile CardPM
+Quote Post

marathdamane91
RE: Works On House Computer But Not School Computer
16 Jun, 2008 - 09:07 AM
Post #3

New D.I.C Head
*

Joined: 1 Apr, 2008
Posts: 9

QUOTE(jjsaw5 @ 16 Jun, 2008 - 10:02 AM) *

if its a game, maybe your school has stuff like this blocked and thats why it's freezing


its a game that im writing for my final and the other people in the class arent having problems with it and i transfered it on a flash drive so its not like im going through a blocke website or anything
User is offlineProfile CardPM
+Quote Post

prajayshetty
RE: Works On House Computer But Not School Computer
16 Jun, 2008 - 09:24 AM
Post #4

D.I.C Head
Group Icon

Joined: 27 Apr, 2007
Posts: 230



Thanked: 1 times
Dream Kudos: 25
My Contributions
QUOTE(marathdamane91 @ 16 Jun, 2008 - 10:07 AM) *

QUOTE(jjsaw5 @ 16 Jun, 2008 - 10:02 AM) *

if its a game, maybe your school has stuff like this blocked and thats why it's freezing


its a game that im writing for my final and the other people in the class arent having problems with it and i transfered it on a flash drive so its not like im going through a blocke website or anything


it doesnt work on mine pc also do u checked it has some runtime error i guess eclipse is printing something in red at runtime
it gives following error dude i cant correct coz i dont have kng of appelet though but me sure other guys will help
guys the error is at runtime

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 7
at ConnectFour.paint(ConnectFour.java:367)
at java.awt.Container.update(Unknown Source)
at sun.awt.RepaintArea.updateComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
the same error is repeated again and again continuously

This post has been edited by prajayshetty: 16 Jun, 2008 - 09:27 AM
User is offlineProfile CardPM
+Quote Post

marathdamane91
RE: Works On House Computer But Not School Computer
16 Jun, 2008 - 09:28 AM
Post #5

New D.I.C Head
*

Joined: 1 Apr, 2008
Posts: 9

QUOTE(prajayshetty @ 16 Jun, 2008 - 10:24 AM) *

QUOTE(marathdamane91 @ 16 Jun, 2008 - 10:07 AM) *

QUOTE(jjsaw5 @ 16 Jun, 2008 - 10:02 AM) *

if its a game, maybe your school has stuff like this blocked and thats why it's freezing


its a game that im writing for my final and the other people in the class arent having problems with it and i transfered it on a flash drive so its not like im going through a blocke website or anything


it doesnt work on mine pc also do u checked it has some runtime error i guess eclipse is printing something in red at runtime
it gives following error dude i cant correct coz i dont have kng of appelet though but i thinkso other guys can help
guys the error is at runtime

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 7
at ConnectFour.paint(ConnectFour.java:367)
at java.awt.Container.update(Unknown Source)
at sun.awt.RepaintArea.updateComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
the same error is repeated again and again continuously

hmm its not even showing up with an error for me at all. and i dont see why it would work at my house but not at my school if its a problem with the program
User is offlineProfile CardPM
+Quote Post

prajayshetty
RE: Works On House Computer But Not School Computer
16 Jun, 2008 - 09:31 AM
Post #6

D.I.C Head
Group Icon

Joined: 27 Apr, 2007
Posts: 230



Thanked: 1 times
Dream Kudos: 25
My Contributions
QUOTE(marathdamane91 @ 16 Jun, 2008 - 10:28 AM) *

QUOTE(prajayshetty @ 16 Jun, 2008 - 10:24 AM) *

QUOTE(marathdamane91 @ 16 Jun, 2008 - 10:07 AM) *

QUOTE(jjsaw5 @ 16 Jun, 2008 - 10:02 AM) *

if its a game, maybe your school has stuff like this blocked and thats why it's freezing


its a game that im writing for my final and the other people in the class arent having problems with it and i transfered it on a flash drive so its not like im going through a blocke website or anything


it doesnt work on mine pc also do u checked it has some runtime error i guess eclipse is printing something in red at runtime
it gives following error dude i cant correct coz i dont have kng of appelet though but i thinkso other guys can help
guys the error is at runtime

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 7
at ConnectFour.paint(ConnectFour.java:367)
at java.awt.Container.update(Unknown Source)
at sun.awt.RepaintArea.updateComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
the same error is repeated again and again continuously

hmm its not even showing up with an error for me at all. and i dont see why it would work at my house but not at my school if its a problem with the program

be patient there are some pro here they will help u when they see ur post
coz me too a learner like u so cant help sorry u executing program directly from command line or using an ide

This post has been edited by prajayshetty: 16 Jun, 2008 - 09:32 AM
User is offlineProfile CardPM
+Quote Post

marathdamane91
RE: Works On House Computer But Not School Computer
16 Jun, 2008 - 09:39 AM
Post #7

New D.I.C Head
*

Joined: 1 Apr, 2008
Posts: 9

so i increased the array and now it works fine. im still just very confused about why it wasn't working at school but was at my house and now i cant get the circles in the right column
User is offlineProfile CardPM
+Quote Post

prajayshetty
RE: Works On House Computer But Not School Computer
16 Jun, 2008 - 09:51 AM
Post #8

D.I.C Head
Group Icon

Joined: 27 Apr, 2007
Posts: 230



Thanked: 1 times
Dream Kudos: 25
My Contributions
QUOTE(marathdamane91 @ 16 Jun, 2008 - 10:39 AM) *

so i increased the array and now it works fine. im still just very confused about why it wasn't working at school but was at my house and now i cant get the circles in the right column

ya it that error also stated something like that just check for errors at runtime and compile time both everytime coz they give someidea of the error

This post has been edited by prajayshetty: 16 Jun, 2008 - 09:52 AM
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Works On House Computer But Not School Computer
16 Jun, 2008 - 10:53 AM
Post #9

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I guess your problem originated at the line:
for (int x = 1; x <= 7; x++)
because your board array is declared only as public int[][] board = new int[7][7]; so it will fail for x==7.
Maybe you weren't running the same version of your program, or it simply didn't get to that line for some reason (I didn't try to understand the logic of your code, only looked at apparent errors).
User is offlineProfile CardPM
+Quote Post

pbl
RE: Works On House Computer But Not School Computer
16 Jun, 2008 - 02:43 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,573



Thanked: 233 times
Dream Kudos: 75
My Contributions
Seriously doubt it works at home biggrin.gif biggrin.gif biggrin.gif

CODE


       public int[][] board = new int[7][7];
...
...
        for (int x = 1; x <= 7; x++)


as soon as x will equal 7 you'll have an array out of bound exception

probably wanted to say: for(int x = i; x < 7; x++)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:10PM