]> Pileus Git - ~andy/spades/blobdiff - src/org/pileus/spades/Cards.java
Fix null pointers and pile parsing
[~andy/spades] / src / org / pileus / spades / Cards.java
index c04e345a5c4eeb25c2bddf318f87aa573f154725..8703c87a4b0b67df59359b3176abd6171195a4e3 100644 (file)
@@ -124,6 +124,7 @@ public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
        private Map<String,Integer> index; // card name to index map
 
        /* Properties */
+       public Spades        game;        // the spades game
        public String[]      hand;        // cards to display
        public String[]      pile;        // played cards to display
 
@@ -143,14 +144,8 @@ public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
 
                this.ylim  = 0.4f;
 
-               this.hand  = new String[] {
-                       "As", "7s", "6s",  "6h", "2h", "Ac",
-                       "Kc", "3c", "10d", "9d", "8d", "7d", "2d"
-               };
-
-               this.pile  = new String[] {
-                       "As", "7s", "6s"
-               };
+               this.hand  = "As Ks Qs Js 10s 9s 8s 7s 6s 5s 4s 3s 2s".split(" ");
+               this.pile  = "Ah Ac Ad".split(" ");
 
                this.index = new HashMap<String,Integer>(52);
                for (int i = 0; i < 52; i++)
@@ -208,7 +203,7 @@ public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
        @Override
        public void onDrawFrame(GL10 unused)
        {
-               Os.debug("Cards: onDrawFrame");
+               //Os.debug("Cards: onDrawFrame");
 
                /* Turn on the program */
                GLES20.glUseProgram(program);
@@ -270,7 +265,7 @@ public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
        @Override
        public boolean onTouchEvent(MotionEvent event)
        {
-               boolean up   = event.getActionMasked() == MotionEvent.ACTION_UP;
+               boolean up = event.getActionMasked() == MotionEvent.ACTION_UP;
 
                float x =    event.getX() / this.getWidth();
                float y = 1-(event.getY() / this.getHeight());
@@ -284,19 +279,19 @@ public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
                        if (this.pick >= num) this.pick = num-1;
                }
                if (y < this.ylim && !this.drag) {
-                       Os.debug("Cards: onTouchEvent - starting drag");
+                       //Os.debug("Cards: onTouchEvent - starting drag");
                        this.drag = true;
                }
                if (this.drag) {
-                       Os.debug("Cards: onTouchEvent - move "
-                                       + x + "," + y);
+                       //Os.debug("Cards: onTouchEvent - move " + x + "," + y);
                        this.requestRender();
                }
                if (y >= this.ylim && this.drag && up) {
-                       Os.debug("Cards: onTouchEvent - playing card");
+                       //Os.debug("Cards: onTouchEvent - playing card");
+                       this.game.onPlay(this.hand[this.pick]);
                }
                if (up) {
-                       Os.debug("Cards: onTouchEvent - ending drag");
+                       //Os.debug("Cards: onTouchEvent - ending drag");
                        this.drag = false;
                }
                return true;
@@ -315,7 +310,7 @@ public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
 
        private int loadTexture(String name)
        {
-               Os.debug("Cards: loadTexture - " + name);
+               //Os.debug("Cards: loadTexture - " + name);
 
                final int[] tex = new int[1];
 
@@ -434,6 +429,8 @@ public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
 
        private void drawCard(String name)
        {
+               if (!this.index.containsKey(name))
+                       return;
                int idx   = this.index.get(name);
                int front = this.face[idx];
                int back  = this.red;