Android Open Source - TwentyEightForAndroid gm Util






From Project

Back to project page TwentyEightForAndroid.

License

The source code is released under:

GNU General Public License

If you think the Android project TwentyEightForAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 *                                                                             *
 * Twenty-Eight for Android is port of popular Asian card game called Rosanne: *
 * Twenty-eight (28) <http://sourceforge.net/projects/rosanne/>. Project       *
 * development is done as NBU Java training course held in Sofia, Bulgaria.    *
 *                                                                             *
 * Copyright (C) 2013-2014 by Todor Balabanov  ( tdb@tbsoft.eu )               *
 *                                                                             *
 * This program is free software: you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by        *
 * the Free Software Foundation, either version 3 of the License, or           *
 * (at your option) any later version.                                         *
 *                                                                             *
 * This program is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
 * GNU General Public License for more details.                                *
 *                                                                             *
 * You should have received a copy of the GNU General Public License           *
 * along with this program. If not, see <http://www.gnu.org/licenses/>.        *
 *                                                                             *
 ******************************************************************************/
/* w  ww .  j  a va2s.c o m*/
package eu.veldsoft.twenty.eight.gm;

import static eu.veldsoft.twenty.eight.common.GlobalSpace;
import eu.veldsoft.twenty.eight.common.GlobalSpace;

//#ifndef _GMUTIL_H_
//#define _GMUTIL_H_

//#include "wx/wxprec.h"

//#ifdef __BORLANDC__
//#pragma hdrstop
//#endif

//#ifndef WX_PRECOMP
//#include "wx/wx.h"
//#endif

public class gmUtil {
  public static int m_value_trans[];
  public static void ShuffleArray(int array[], long n){}
  
  public static String PrintLong(long cards) {  
    int i;
    String out, last;
  
    //wxLogDebug(String.Format("Cards = %lu", cards));
  
    out = ("");
    //wxLogDebug(String.Format("%s%s", m_suits[highest / 8], m_values[highest % 8]));
    for(i = 0; i < 32; i++)
    {
      if((cards & (1 << i)) != 0)
        out = out + m_suits[i / 8] + m_values[i % 8] + (",");
    }
    if(!out.isEmpty())
    {
      last = out.substring(0, out.length() - 1);
    }
  
    return last;
  }
  
  public static String PrintHands(long hands[]) {
    String ret_val;
    String last;
    String out;
    long temp;
    int i, j;

    ret_val = "";

    // Print North first
    //wxLogMessage(SPACES20 + m_long_locs[2]);
    ret_val += GlobalSpace.SPACES20 + m_long_locs[2];
    ret_val += "\n";

    for (i = 0; i < 4; i++)
    {
      temp = (hands[2] & m_suit_mask[i]) >> m_suit_rs[i];
      out = String.Format(("%s - "), m_suits[i]);
      for(j = 7; j >= 0; j--)
      {
        if((temp & (1 << j)) != 0)
          out = out + m_values[j % 8] + (",");
      }

      last = out.Left(out.length() - 1);
      //wxLogMessage(SPACES20 + final);
      ret_val.Append(GlobalSpace.SPACES20 + last);
      ret_val.Append(("\n"));
    }

    // Print East and West in the same line :D
    //wxLogMessage(String.Format("%-40s%-40s", m_long_locs[1], m_long_locs[3]));
    ret_val.Append(String.Format(("%-40s%-40s"), m_long_locs[1].c_str(), m_long_locs[3].c_str()));
    ret_val.Append(("\n"));

    for (i = 0; i < 4; i++)
    {
      temp = (hands[1] & m_suit_mask[i]) >> m_suit_rs[i];
      out = String.Format(("%s - "), m_suits[i].c_str());
      //for(j = 0; j < 8; j++)
      for(j = 7; j >= 0; j--)
      {
        if(temp & (1 << j))
          out = out + m_values[j % 8] + _(",");
      }

      last = String.Format(("%-40s"), out.Left(out.Length() - 1).c_str());

      temp = (hands[3] & m_suit_mask[i]) >> m_suit_rs[i];
      out = String.Format(("%s - "), m_suits[i].c_str());
      //for(j = 0; j < 8; j++)
      for(j = 7; j >= 0; j--)
      {
        if(temp & (1 << j))
          out = out + m_values[j % 8] + _(",");
      }

      last += String.Format(("%-40s"), out.Left(out.Length() - 1).c_str());

      //wxLogMessage(final);
      ret_val.Append(last);
      ret_val.Append(("\n"));
    }

    // Finally print South
    //wxLogMessage(SPACES20 + m_long_locs[0]);
    ret_val.Append(SPACES20 + m_long_locs[0]);
    ret_val.Append(("\n"));

    for (i = 0; i < 4; i++)
    {
      temp = (hands[0] & m_suit_mask[i]) >> m_suit_rs[i];
      out = String.Format(("%s - "), m_suits[i].c_str());
      //for(j = 0; j < 8; j++)
      for(j = 7; j >= 0; j--)
      {
        if(temp & (1 << j))
          out = out + m_values[j % 8] + _(",");
      }

      last = out.Left(out.Length() - 1);
      //wxLogMessage(SPACES20 + final);
      ret_val.Append(SPACES20 + last);
      ret_val.Append(("\n"));
    }
    return ret_val;
  }
  
  public static String m_suits[];
  public static String m_values[];
  public static String m_short_locs[];
  public static String m_long_locs[];
  public static int m_suit_mask[];
  public static long m_suit_rs[];
  public static int m_points[];
  public static int m_total_points[];
  public static String m_short_teams[];
  public static int GetCardIndex(String text) {return 0}
  public static boolean SetStatusText(final String& text, int i = 0);
  public static int BitsSetTable256[];
  public static int LogTable256[];
  public static long CountBitsSet(long v) {}
  public static long HighestBitSet(long v) {}
};

//#endif



//




//




//




// Part of the code borrowed from
// http://graphics.stanford.edu/~seander/bithacks.html

//#include "gm/gmutil.h"
//#include "SFMT.h"
//#include "SFMT-params.h"

//int gmUtil.m_value_trans[] = {5, -1, -1, -1, -1, -1, 0, 1, 6, 4, 7, 2, 3};
int gmUtil.m_value_trans[] = {6, 7, 11, 12, 9, 0, 8, 10};
String gmUtil.m_suits[] = {("C"), ("D"), ("H"), ("S")};
String gmUtil.m_values[] = {("7"), ("8"), ("Q"), ("K"), ("10"), ("A"), ("9"), ("J")};
String gmUtil.m_short_locs[] = {("S"), ("W"), ("N"), ("E")};
String gmUtil.m_long_locs[] = {("South"), ("West"), ("North"), ("East")};
long gmUtil.m_suit_mask[] = {0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000};
long gmUtil.m_suit_rs[] = {0, 8, 16, 24};
int gmUtil.m_points[] = {0, 0, 0, 0, 1, 1, 2, 3};
int gmUtil.m_total_points[] = {
  0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1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 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};

//Fisher-Yates shuffle
//http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
//
//(For a zero-based array)
//To shuffle an array a of n elements:
//   for i from n - 1 downto 1 do
//         j ? random integer with 0 <= j <= i
//         exchange a[j] and a[i]

void gmUtil.ShuffleArray(int *array, long n)
{

  if (n > 1) {
    long i;
    for (i = n - 1; i >= 1; i--) {
        // TODO : Non linear distribution. To be corrected.
      long j = ((long)gen_rand32()) % (i + 1);
      int t = array[j];
      array[j] = array[i];
      array[i] = t;
    }
  }
}

int gmUtil.GetCardIndex(String text)
{
  String suit, value;
  int x, y;
  suit = text.Left(1);
  //wxPrintf(suit + "\n");
  value = text.Mid(1);
  //wxPrintf(value + "\n");

  if(!suit.CmpNoCase(("C")))
    x = 0;
  else if(!suit.CmpNoCase(("D")))
    x = 1;
  else if(!suit.CmpNoCase(("H")))
    x = 2;
  else if(!suit.CmpNoCase(("S")))
    x = 3;
  else
    return -1;

  if(!value.CmpNoCase(("7")))
    y = 0;
  else if(!value.CmpNoCase(("8")))
    y = 1;
  else if(!value.CmpNoCase(("Q")))
    y = 2;
  else if(!value.CmpNoCase(("K")))
    y = 3;
  else if(!value.CmpNoCase(("10")))
    y = 4;
  else if(!value.CmpNoCase(("A")))
    y = 5;
  else if(!value.CmpNoCase(("9")))
    y = 6;
  else if(!value.CmpNoCase(("J")))
    y = 7;
  else
    return -1;

  return (x * 8) + y;
}
boolean gmUtil.SetStatusText(final String& text, int i)
{
  wxFrame *main_frame;
  wxStatusBar *status_bar;

  main_frame = null;
  main_frame = (wxFrame *)wxTheApp.GetTopWindow();
  if(!main_frame)
    return false;

  status_bar = null;
  status_bar = main_frame.GetStatusBar();
  if(!status_bar)
    return false;

  status_bar.SetStatusText(text, i);
  status_bar.Update();
  return true;
}



long gmUtil.CountBitsSet(long v)
{
  return BitsSetTable256[v & 0xff] +
    BitsSetTable256[(v >> 8) & 0xff] +
    BitsSetTable256[(v >> 16) & 0xff] +
    BitsSetTable256[v >> 24];
  //int final w = v - ((v >> 1) & 0x55555555);                    // temp
  //int final x = (w & 0x33333333) + ((w >> 2) & 0x33333333);     // temp
  //int final c = ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
  //return c;
}

long gmUtil.HighestBitSet(long v)
{
  register long t, tt; // temporaries
  if (tt = v >> 16)
  {
    return (t = v >> 24) ? 24 + LogTable256[t] : 16 + LogTable256[tt & 0xFF];
  }
  else
  {
    return (t = v >> 8) ? 8 + LogTable256[t] : LogTable256[v];
  }
}




Java Source Code List

eu.veldsoft.twenty.eight.MainActivity.java
eu.veldsoft.twenty.eight.ai.aiAgent.java
eu.veldsoft.twenty.eight.ai.aiEval.java
eu.veldsoft.twenty.eight.ai.aiMove.java
eu.veldsoft.twenty.eight.ai.aisuitlengthsolver.java
eu.veldsoft.twenty.eight.ai.ut_suitlengthsolver.java
eu.veldsoft.twenty.eight.common.GlobalSpace.java
eu.veldsoft.twenty.eight.gg.ggcard.java
eu.veldsoft.twenty.eight.gg.ggpanel.java
eu.veldsoft.twenty.eight.gm.gmEngineData.java
eu.veldsoft.twenty.eight.gm.gmUtil.java
eu.veldsoft.twenty.eight.gm.gmengine.java
eu.veldsoft.twenty.eight.gm.gmrand.java
eu.veldsoft.twenty.eight.ra.ra.java
eu.veldsoft.twenty.eight.ra.rabid.java
eu.veldsoft.twenty.eight.ra.racommon.java
eu.veldsoft.twenty.eight.ra.raconfig.java
eu.veldsoft.twenty.eight.ra.radlgabout.java
eu.veldsoft.twenty.eight.ra.radlgprefs.java
eu.veldsoft.twenty.eight.ra.radlgrules.java
eu.veldsoft.twenty.eight.ra.raevents.java
eu.veldsoft.twenty.eight.ra.ragamepanel.java
eu.veldsoft.twenty.eight.ra.rainfo.java
eu.veldsoft.twenty.eight.ra.ramain.java
eu.veldsoft.twenty.eight.ra.raplayer.java
eu.veldsoft.twenty.eight.ra.raupdate.java