The Artima Developer Community
Sponsored Link

.NET Buzz Forum
CodeZone Duel

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Sascha Corti

Posts: 797
Nickname: sascha
Registered: Aug, 2003

Sascha Corti is a developer evangelist for Microsoft in Switzerland.
CodeZone Duel Posted: Jun 24, 2004 8:21 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Sascha Corti.
Original Post: CodeZone Duel
Feed Title: Console.WriteLine("Hello World");
Feed URL: http://www.corti.com/WebLogSascha/blogxbrowsing.asmx/GetRss?
Feed Description: A technology blog with a focus on the .NET framework, the Visual Studio .NET tools and the Windows server platform with of course the normal weblog-noise on what's happening in the industry and reviews of the latest geeky gadgets.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Sascha Corti
Latest Posts From Console.WriteLine("Hello World");

Advertisement

The local web service development contest held at CodeZone.ch was a big success. Although the game that we used for the contest (rock, paper, scissors) appeared to be too simple at first, it turned out to be very good for the contest as simple programs, put together in a few minutes, based on a simple random function could participate equally as more sophisticated algorithms that tried to predict the next move of the opponent, as the participating services played 25 rounds against the same opponent in a row. At first, I only mentioned rock, paper scissors as possible game for the contest to illustrate my idea of how competing web services with a central game web service would work and never meant for it to be used in the real contest... :)

Congratulations to Frigidor who won the contest!

I had a chance to talk to him and ask him for the algorithm he used and was pleased to hear, that we both used somewhat similar ones (even though my service didn't compete as I wasn't allowed to play).

Here is my code:

using System;
using System.IO;
using System.Data;
using System.Diagnostics;
using System.Security.Cryptography;

namespace ClientService1.classes
{
  /// <summary>
  /// Summary description for Player1.
  /// </summary>
  public class Player1
  {
    private Player1_DS ds;
    private string path;
    private int gameId;

    public Player1(string sPath)
    {
      path = sPath;
      ds = new Player1_DS();
    }

    public Player1(string sPath, int iGameId)
    {
      path = sPath;
      gameId = iGameId;
      ds = new Player1_DS();

      LoadData();
    }

    public void ShakeHands(int iGameId)
    {
      gameId = iGameId;
      Player1_DS.Player1TableRow p1r = ds.Player1Table.NewPlayer1TableRow();
                  
      p1r.BeginEdit();
      p1r.GameId = gameId;
      p1r.GameOpen = true;
      p1r.RockPlayed = 0;
      p1r.PaperPlayed = 0;
      p1r.ScissorsPlayed = 0;
      // p1r.LastTurnPlayed = "";
      p1r.TurnsPlayed = 0;
      // p1r.Turns = "";
      // p1r.GameWon = "";

      p1r.EndEdit();

      ds.Player1Table.AddPlayer1TableRow(p1r);
      
      SaveData();
    }

    public Enums.GameOptions TakeTurn()
    {
      DataRow ds_row = ds.Player1Table.Rows[0];

      ds_row["TurnsPlayed"] = Convert.ToInt32(ds_row["TurnsPlayed"])+1;

      Enums.GameOptions o;

      int r = Convert.ToInt32(ds_row["RockPlayed"]);
      int p = Convert.ToInt32(ds_row["PaperPlayed"]);
      int s = Convert.ToInt32(ds_row["ScissorsPlayed"]);

      if ( r > p )
      {
        if ( r > s )
          o = Enums.GameOptions.Paper;
        else
        {
          if ( r == p & p == s )
            o = GetRandom();
          else
            o = Enums.GameOptions.Rock;
        }
      }
      else
      {
        if ( p > s )
          o = Enums.GameOptions.Scissors;
        else
        {
          if ( r == p & p == s )
            o = GetRandom();
          else
            o = Enums.GameOptions.Rock;
        }
      }
      
      ds_row["LastTurnPlayed"] = o.ToString();

      SaveData();

      return o;
    }

    public void ReceiveTurnFeedback(Enums.GameOptions opponentOption)
    {
      Enums.GameOptions o;
      string s = "";

      DataRow ds_row = ds.Player1Table.Rows[0];

      switch ( ds_row["LastTurnPlayed"].ToString() )
      {
        case "Rock":
          o = Enums.GameOptions.Rock;
          break;
        case "Paper":
          o = Enums.GameOptions.Paper;
          break;
        default
          o = Enums.GameOptions.Scissors;
          break;
      }

      if ( opponentOption == Enums.GameOptions.Rock )
      {
        ds_row["RockPlayed"] = Convert.ToInt32(ds_row["RockPlayed"])+1;

        if ( o == Enums.GameOptions.Rock )
          s = "0";
        if ( o == Enums.GameOptions.Paper )
          s = "+";
        if ( o == Enums.GameOptions.Scissors )
          s = "-";
      }
      if ( opponentOption == Enums.GameOptions.Paper )
      {
        ds_row["PaperPlayed"] = Convert.ToInt32(ds_row["PaperPlayed"])+1;

        if ( o == Enums.GameOptions.Rock )
          s = "-";
        if ( o == Enums.GameOptions.Paper )
          s = "0";
        if ( o == Enums.GameOptions.Scissors )
          s = "+";
      }
      if ( opponentOption == Enums.GameOptions.Scissors )
      {
        ds_row["ScissorsPlayed"] = Convert.ToInt32(ds_row["ScissorsPlayed"])+1;

        if ( o == Enums.GameOptions.Rock )
          s = "+";
        if ( o == Enums.GameOptions.Paper )
          s = "-";
        if ( o == Enums.GameOptions.Scissors )
          s = "0";
      }

      ds_row["Turns"] = ds_row["Turns"].ToString()+s;

      SaveData();
    }

    public void ReceiveGameFeedback(bool youWin)
    {
      DataRow ds_row = ds.Player1Table.Rows[0];

      if ( youWin )
        ds_row["GameWon"] = "yes";
      else
        ds_row["GameWon"] = "no";

      ds_row["GameOpen"] = false;

      SaveData();
    }

    private Enums.GameOptions GetRandom()
    {
      byte[] random = new Byte[1];

      RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
      rng.GetBytes(random);

      int r = (random[0] % 3) + 1;

      switch ( r )
      {
        case 1:
          return Enums.GameOptions.Rock;
          break;
        case 2:
          return Enums.GameOptions.Paper;
          break;
        default:
          return Enums.GameOptions.Scissors;
          break;
      }
    }

    private void LoadData()
    {
      try
      {
        ds.ReadXml(path+"Player1_"+gameId.ToString("00000000")+".xml");
      }
      catch
      {
        ds = new Player1_DS();
      }
    }

    private void SaveData()
    {
      try
      {
        ds.WriteXml(path+"Player1_"+gameId.ToString("00000000")+".xml");
      }
      catch( Exception ex )
      {
        Debug.WriteLine("Error encountered saving data: " + ex.ToString());
      }
    }
  }
}

 

Read: CodeZone Duel

Topic: Now an Eight Previous Topic   Next Topic Topic: Speaking to Boston .Net Users Group on Security

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use