New Posts  All Forums:Forum Nav:

Code defies logic?

post #1 of 9
Thread Starter 
This is really getting to me at the moment. This code defies the logic I set for.... or maybe I'm just missing something right in front of me? (most likely the latter!)

This is the code for a gamepad to execute the code. I've used this version of edge detection all the way through my project without a problem and it works every time on everything else
Code:

                #region Press A To call Snap

                if (mPlayerOne.Buttons.A == ButtonState.Pressed)
                {
                    mButtonAHasBeenPressedPlayerOne = true;
                }

                if ((mPlayerOne.Buttons.A == ButtonState.Released) && mButtonAHasBeenPressedPlayerOne == true)
                {
                    mButtonAHasBeenPressedPlayerOne = false;
                    mPlayerOneSnapped = true;
                    SnapPlayerOne();
                    
                }

                #endregion

This is the method that is executed when the button on the gamepad is released:
Code:
        private void SnapPlayerOne()
        {
            // If Player One presses Snap, but Player Two hasn't
            if (mPlayerOneSnapped == true && mPlayerTwoSnapped == false && mTimerStarted == false)
            {
                snapTime = 500; // Start a timer
                mTimerStarted = true; // Indicate the timer has been started
            }
            // If both Players snap, move to the pool
            if (mPlayerOneSnapped == true & mPlayerTwoSnapped == true)
            {
                snapTime = 0;
                mTimerStarted = false;
                mPlayerOneSnapped = false;
                MoveToPool();
            }
            //If no one snaps, Player One gets the pile
            if (mPlayerOneSnapped == true && mPlayerTwoSnapped == false && snapTime <= 0 && mTimerStarted == true)
            {
                snapTime = 0;
                mTimerStarted = false;

                if (playerOne.Pile.GetLength() == 0 || playerTwo.Pile.GetLength() == 0)
                {
                    //Do nothing, to stop Null exceptions
                }
                else if (playerOne.GetCardValue(playerOne.Pile.cards[0]) == playerTwo.GetCardValue(playerTwo.Pile.cards[0]))
                {
                    MovePileToStackPlayerOne();
                    mPlayerOneSnapped = false;
                }
                else if (playerOne.GetCardValue(playerOne.Pile.cards[0]) != playerTwo.GetCardValue(playerTwo.Pile.cards[0]))
                {
                    MoveToPool();
                    mPlayerOneSnapped = false;
                }
            }      
        }

It's this very code that is causing the problem. For some reason it will not "MoveStackToPile" until the SECOND press of the button. But no-where is it told to rely on a second button press.

What should happen is this:

Button is pushed and snapped is set to true

if p1 snap is true and p2 snap is false, then start a timer at 500 milliseconds

if the timer gets to 0 and no other player has snapped, then execute the code.

I debug the code line by line. When I hit the button it does indeed execute the large chunk of code, and start a timer. I can then watch the timer fall form 500 to 0 in the debugging. Yet, when the timer gets to 0 nothing happens, whereas it should. For some reason reason nothing happens until I hit the button again and I can't figure out why!
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #2 of 9
If I am understanding correctly:

if(true and false)
start timer at 500 ms
if(true and true)
movestuff
if true and false and timer done
move stuff

So say it is true and false, so the first if statement is true, timer gets started, second if statement is false, third if statement is false because the timer hasn't finished yet, method finished.

You press the button a second time, the method is activated again, this time there is already a timer so first if statement is false, and ether the second or third is true.

The code does not stop with your timer, so the method finishes executing before the timer ends.

At least, that is how things appear from my end
post #3 of 9
Thread Starter 
That makes sense, but how can I force the method to stay open until the timer is either finished or the other player presses?
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #4 of 9
Thread Starter 
Code:
                //If there is nothing on the pile at all
                if (playerOne.Pile.GetLength() == 0 && playerTwo.Pile.GetLength() == 0)
                {
                    //Do nothing
                }
                    //if there IS something on the pile
                else if (playerOne.Pile.GetLength() >= 1 && playerTwo.Pile.GetLength() >= 1)
                {
                    //if it's not possible to snap, set to false
                    if (playerOne.GetCardValue(playerOne.Pile.cards[0]) != playerTwo.GetCardValue(playerTwo.Pile.cards[0]))
                    {
                        mCPUSnapPossible = false;
                    }
                    // If it is possible to snap then set to true and start the timer
                    else if (playerOne.GetCardValue(playerOne.Pile.cards[0]) == playerTwo.GetCardValue(playerTwo.Pile.cards[0]) && mCPUSnapPossible == false)
                        {
                            mCPUSnapPossible = true;
                            mCPUTime = 15000;
                        }
                    
  
                    // If it's possible to snap and the timer has elapsed, CPU calls snap (as player two)
                    if (mCPUSnapPossible == true && mCPUTime <= 0)
                        {
                            mCPUTime = 0;
                            SnapPlayerTwo();
                            
                        }
                }

Some more code using the same way of using a timer...but this code works flawlessly frown.gif
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #5 of 9
What I might give as a try would be something like:
Code:
private void SnapPlayerOne()
        {
            // If Player One presses Snap, but Player Two hasn't
            if (mPlayerOneSnapped == true && mPlayerTwoSnapped == false && mTimerStarted == false)
            {
                snapTime = 500; // Start a timer
                mTimerStarted = true; // Indicate the timer has been started
                while(snapTime > 0 &&  mPlayerTwoSnapped == false)
                {
                        //We need to wait either 500 ms or until player two snaps
                }
            //Continue method, if mPlayerTwoSnapped is true do the first if statement, else do the second
            }

Not 100% sure this would work, mainly depends on how your timer is working, if the timer and this code are in the same thread than that while loop might be an infinite loop.
post #6 of 9
Thread Starter 
IT does indeed seem to be an infinite loop frown.gif
Code:
 // If Player One presses Snap, but Player Two hasn't
            if (mPlayerOneSnapped == true && mPlayerTwoSnapped == false && mTimerStarted == false)
            {
                snapTime = 2000; // Start a timer
                mTimerStarted = true; // Indicate the timer has been started

                while (snapTime > 0)
                {
                    // If both Players snap, move to the pool
                    if (mPlayerOneSnapped == true & mPlayerTwoSnapped == true)
                    {
                        snapTime = 0;
                        mTimerStarted = false;
                        mPlayerOneSnapped = false;
                        MoveToPool();
                    }

                    //If no one snaps, Player One gets the pile
                    if (mPlayerOneSnapped == true && mPlayerTwoSnapped == false && snapTime <= 0 && mTimerStarted == true)
                    {
                        snapTime = 0;
                        mTimerStarted = false;

                        if (playerOne.Pile.GetLength() == 0 || playerTwo.Pile.GetLength() == 0)
                        {
                            //Do nothing, to stop Null exceptions
                        }
                        else if (playerOne.GetCardValue(playerOne.Pile.cards[0]) == playerTwo.GetCardValue(playerTwo.Pile.cards[0]))
                        {
                            MovePileToStackPlayerOne();
                            mPlayerOneSnapped = false;
                        }
                        else if (playerOne.GetCardValue(playerOne.Pile.cards[0]) != playerTwo.GetCardValue(playerTwo.Pile.cards[0]))
                        {
                            MoveToPool();
                            mPlayerOneSnapped = false;
                        }
                    }      
                }
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #7 of 9
Thread Starter 
I forgot to mention that the timer is based on the base.update gameTime in milliseconds
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #8 of 9
I think it was just a syntactical error. You used bitwise & in your second if statement, rather than logical &&:
Code:
// If both Players snap, move to the pool
if (mPlayerOneSnapped == true & mPlayerTwoSnapped == true) // <-- should be && rather than &
     
CPUMotherboardGraphicsRAM
Intel Core i7-4770K Gigabyte G1.Sniper M5 EVGA GTX 780 SC Crucial Ballistix Sport 2 x 8GB 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 840 Pro 256GB Seagate Barracuda 3TB Seagate Barracuda 4TB Asus BD-ROM 
CoolingOSMonitorKeyboard
Noctua NH-D14 Windows 8 Pro Dell U2713HM Ducky YOTD (MX Brown) 
PowerCaseMouseMouse Pad
SeaSonic SS-760XP2 Fractal Design Arc Mini Roccat Savu SteelSeries 9HD 
Audio
Beyerdynamic DT990 250 Ohm 
CPUMotherboardGraphicsRAM
AMD A6-3500 Asrock A75M HD 6530D Samsung 4GB DDR3 1600  
Hard DriveHard DriveHard DriveOptical Drive
Intel 320 80GB Samsung Spinpoint F4 2TB WD Caviar Green 3TB Sony BD-Rom 
CoolingOSMonitorKeyboard
Noctua NH-C12P SE14 Win 7 Professional 64-bit Samsung LN46C600 Lenovo N5902 
PowerCaseOther
Corsair CX430 Antec Veris Fusion HDHomeRun Prime 
CPUMotherboardGraphicsRAM
AMD Phenom II X3 B55 Asus M5A99X EVO Sapphire HD 7950 G.Skill 8GB DDR3 
Hard DriveOptical DriveCoolingMonitor
Samsung F3 1TB LG DVD-RW CM Hyper 212 Plus Dell U2312HM 
KeyboardPowerCaseMouse
Pink KBT Race (MX Blue) Lepa G700-MA Corsair 500R Razer Abyssus 
Mouse Pad
Razer Goliathus Speed 
  hide details  
Reply
     
CPUMotherboardGraphicsRAM
Intel Core i7-4770K Gigabyte G1.Sniper M5 EVGA GTX 780 SC Crucial Ballistix Sport 2 x 8GB 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 840 Pro 256GB Seagate Barracuda 3TB Seagate Barracuda 4TB Asus BD-ROM 
CoolingOSMonitorKeyboard
Noctua NH-D14 Windows 8 Pro Dell U2713HM Ducky YOTD (MX Brown) 
PowerCaseMouseMouse Pad
SeaSonic SS-760XP2 Fractal Design Arc Mini Roccat Savu SteelSeries 9HD 
Audio
Beyerdynamic DT990 250 Ohm 
CPUMotherboardGraphicsRAM
AMD A6-3500 Asrock A75M HD 6530D Samsung 4GB DDR3 1600  
Hard DriveHard DriveHard DriveOptical Drive
Intel 320 80GB Samsung Spinpoint F4 2TB WD Caviar Green 3TB Sony BD-Rom 
CoolingOSMonitorKeyboard
Noctua NH-C12P SE14 Win 7 Professional 64-bit Samsung LN46C600 Lenovo N5902 
PowerCaseOther
Corsair CX430 Antec Veris Fusion HDHomeRun Prime 
CPUMotherboardGraphicsRAM
AMD Phenom II X3 B55 Asus M5A99X EVO Sapphire HD 7950 G.Skill 8GB DDR3 
Hard DriveOptical DriveCoolingMonitor
Samsung F3 1TB LG DVD-RW CM Hyper 212 Plus Dell U2312HM 
KeyboardPowerCaseMouse
Pink KBT Race (MX Blue) Lepa G700-MA Corsair 500R Razer Abyssus 
Mouse Pad
Razer Goliathus Speed 
  hide details  
Reply
post #9 of 9
Thread Starter 
I spotted that just after I posted the code. I think I'd also incorrectly put a single = instead of == at one point frown.gif After correcting those two things it still give the same result!
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming