воскресенье, 1 июня 2014 г.

Unity game Abstract Reality Games "INSECTUM"

Abstract Reality Games "INSECTUM" v1.0 Build 40510

Insect world simulator, fly & live in green world.
Features:
Full 3d
A.L.A.I system (Artificial Life Active Interaction)
-Active unit:Hornet

Other units:
-Grey robberfly
-Brown robberfly
-Black_wasp,
-Apterus.
-Butterfly
-Blue moth
-Spider_wolf
-Wild_bee
-Dragonfly
-Mantis
-Caterpillar
-Green caterpillar
-Grasshopper
-Leperina
Open world
Three locations:
-Meadow
-Cane labyrinth
-Wild bee nest
Fly simulation mode
Hunt simulation mode
Health and damage system
Insect migration
Food competition
Nest building
Larvae Feeding
and more..






Play INSECTUM Unity game online

вторник, 5 ноября 2013 г.

Puzzle game for Unity3D in C#

The idea of this 2D game is as follows: Given a 800*600 image, a custom unity plugin splits into pieces (say, 4) in edit mode and then randomly shuffles pieces around the gameboard. In run-time user has to drag and drop pieces into its original position with mouse. If original image is assembled, the goal of the game is achieved and game setup is restarted (pieces shuffled again).

Play puzzle Unity game onlne

Download puzzle game Unity source code 

Script that handles game logic
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System;

public class DragIt : MonoBehaviour {
 
 public GameObject winImage;
 
 private Ray ray;
 private RaycastHit hit;
 
 private bool dragging=false;
 private GameObject draggingObject = null;
 private GameObject targetObject = null;
 private Color savedColor;
 
 // Update is called once per frame
 void Update () {
  if (Input.GetMouseButton(0)) {
   ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   if (Physics.Raycast(ray, out hit)) {
    
    if (!dragging) {
     dragging = true;
     draggingObject = hit.transform.gameObject;
    } 
    if (dragging) {
     draggingObject.transform.position = new Vector3(hit.point.x,hit.point.y,200);
     
     // calculate distance to all pieces, except current object
     var pieces = Helper.IteratePieces().Where (p=>p!=draggingObject).ToList();
     var distances = pieces.Select(p=>Vector3.Distance(p.transform.position,draggingObject.transform.position)).ToList();
     
     var minDistance = distances.Min(); // get minimum distance
     
     // check distance between current position and original position 
     PositionData storage = draggingObject.GetComponent();
     var currentDistance = Vector3.Distance(storage.ShuffledPosition,draggingObject.transform.position);
     GameObject targetObjectNew=null;
     if (currentDistance < minDistance)
      targetObjectNew = draggingObject;
     else {
      var index = distances.IndexOf(minDistance);
      targetObjectNew = pieces[index];
     }
     
     //Debug.Log("Distances: " + Helper.ToString(distances) + ", target " + targetObjectNew.name + " min distance: " + minDistance + ", current distance: " + currentDistance);
          
     
     // target object changed
     if (targetObjectNew != targetObject) {
      //Debug.Log("Distances: " + Helper.ToString(distances) + ", target " + targetObjectNew.name + " min distance: " + minDistance + "current distance: " + currentDistance);
      Debug.Log("Switch to  "  + targetObjectNew.name );
      if (targetObject!=null && targetObject != draggingObject ) {
       targetObject.renderer.material.color = savedColor;
       //Debug.Log("Restored color for "  + targetObject.name + " to " + savedColor.ToString() );
      }
      
      if (targetObjectNew != draggingObject) {
       
       // highlight target object
       savedColor = targetObjectNew.renderer.material.color;
       targetObjectNew.renderer.material.color = Color.green;
      }
      
      targetObject = targetObjectNew;
     } 
     
     
     
    }
    
   }
  } else if (dragging) {
   dragging = false; // dropping
   PositionData storageSource = draggingObject.GetComponent();
   
   
   
   // reset target object position, if it is the same object
   if (targetObject==draggingObject)
    draggingObject.transform.position = storageSource.ShuffledPosition;
   else {
    // restore color
    if (targetObject!=null)
     targetObject.renderer.material.color = savedColor;
    PositionData storageTarget = targetObject.GetComponent();
    
    // swap positions between source and target
    targetObject.transform.position = storageSource.ShuffledPosition;
    draggingObject.transform.position = storageTarget.ShuffledPosition;
    
    // store new positions
    storageSource.ShuffledPosition = draggingObject.transform.position;
    storageTarget.ShuffledPosition = targetObject.transform.position;
    
    // check win condition and notify by color blinking
    if (!CheckWinCondition()) 
    {
     if (draggingObject.transform.position == storageSource.OriginalPosition)
      StartCoroutine(Blink (Color.green, draggingObject));
     else
      StartCoroutine(Blink (Color.red, draggingObject));
    
     if (targetObject.transform.position == storageTarget.OriginalPosition)
      StartCoroutine( Blink (Color.green, targetObject));
     else
      StartCoroutine(Blink (Color.red, targetObject));
    }
    
   }
   Debug.Log("Dropped  "  + targetObject.name );
   targetObject = null;
   draggingObject = null;
  }
  
  
  
 
 }
 
 bool CheckWinCondition() {
  bool win = Helper.IteratePieces().Select(o=>o.GetComponent()).All(d=>d.OriginalPosition==d.ShuffledPosition);
  if (win) {
   StartCoroutine(Win ());
  }
  return win;
 }
 
 /// 
 /// Blink with the specified color for a given game object.
 /// 
 /// 
 /// Color.
 /// 
 /// 
 /// Go.
 /// 
 IEnumerator Blink(Color color, GameObject go) {
  Color savedcolor = go.renderer.material.color;
  for(int i=0;i<=2;i++) {
   yield return new WaitForSeconds(.1f);
   go.renderer.material.color = color;
   yield return new WaitForSeconds(.25f);
   go.renderer.material.color = savedcolor;
  }
 }
 
 IEnumerator Win() {
  Helper.IteratePieces().ToList().ForEach(o=>StartCoroutine(Blink (Color.green, o)));
  yield return new WaitForSeconds(.2f);
  
  if (winImage!=null)
   winImage.SetActive(true);
  else
   Debug.LogError("WinImage not found");
  
  // enjoy a "GAME WON" title for a moment and restart a game
  yield return new WaitForSeconds(5f);
  if (winImage!=null)
   winImage.SetActive(false);
  Helper.ShuffleGameObjects();
 }
}


Unity plugin source code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System;
using System.Linq;

public class CreatePuzzle : ScriptableWizard {

 public Texture2D Picture;
 
 public int N=2;
 
 [MenuItem("GameObject/Create Other/Puzzle")]
    static void CreateWizard()
    {
        ScriptableWizard.DisplayWizard("Create Puzzle",typeof(CreatePuzzle));
    }
 
 void OnWizardCreate()
 {
  int width = Picture.width;
  int height = Picture.height;
  Debug.Log("wh: " + width + "/" + height);
    
  for(int x=1;x<=N;x++)
   for(int y=1;y<=N;y++) {
    int xPiece = Convert.ToInt32(width*x/(float)N - width/2f - width/2f/N);
    int yPiece = Convert.ToInt32(height*y/(float)N - height/2f - height/2f/N);
    string name = "piece_" + x + "_" + y; 
    //Debug.Log(name + ": " + xPiece + "/" + yPiece);
    Rect uv = new Rect( (x-1)/(float)N,(y-1)/(float)N, 1/(float)N, 1/(float)N);
    CreateMesh(name,xPiece,yPiece,uv, width/N, height/N);
  }
  
  Shuffle(); // shuffle pieces to make a puzzle
 }
 
 GameObject CreateMesh(string meshName, int x, int y, Rect uv, int width, int height) {
  
  string assetFolder = "Assets";
  
  
  int anchorX=Convert.ToInt32(width/2);
  int anchorY=Convert.ToInt32(height/2);
   
  //Create Vertices
  Vector3[] Vertices = new Vector3[4];
  
  //Create UVs
  Vector2[] UVs = new Vector2[4];
  
  //Two triangles of quad
  int[] Triangles = new int[6];
  
  //Assign vertices based on pivot
  
  //Bottom-left
  Vertices[0].x = -anchorX;
  Vertices[0].y = -anchorY;
  
  //Bottom-right
  Vertices[1].x = Vertices[0].x+width;
  Vertices[1].y = Vertices[0].y;
   
  //Top-left
  Vertices[2].x = Vertices[0].x;
  Vertices[2].y = Vertices[0].y+height;
  
  //Top-right
  Vertices[3].x = Vertices[0].x+width;
  Vertices[3].y = Vertices[0].y+height;
  
  Vector2[] uvs2 = new Vector2[]
        {
            new Vector2(uv.x, uv.y),      //Bottom-left
            new Vector2(uv.x+uv.width, uv.y),    //Bottom-right
            new Vector2(uv.x, uv.y+uv.height),    //Top-left
            new Vector2(uv.x+uv.width, uv.y+uv.height),  //Top-right 
        };
  
  //Assign triangles
  Triangles[0]=3;
  Triangles[1]=1;
  Triangles[2]=2;
  
  Triangles[3]=2;
  Triangles[4]=1;
  Triangles[5]=0;
  
  //Generate mesh
  Mesh mesh = new Mesh();
  mesh.name = meshName;
  mesh.vertices = Vertices;
  mesh.uv = uvs2;
  mesh.triangles = Triangles;
  mesh.RecalculateNormals();
  
  //Create asset in database
  AssetDatabase.CreateAsset(mesh, AssetDatabase.GenerateUniqueAssetPath(assetFolder + "/" + meshName) + ".asset");
        AssetDatabase.SaveAssets();
  
  //Create plane game object
  GameObject piece = new GameObject(meshName);
  MeshFilter meshFilter = (MeshFilter)piece.AddComponent(typeof(MeshFilter));
  piece.AddComponent(typeof(MeshRenderer));
  
  
  //Assign mesh to mesh filter
  meshFilter.sharedMesh = mesh;
  
  mesh.RecalculateBounds();
  
  //Add a box collider component
  piece.AddComponent(typeof(BoxCollider));
  
  // place piece into designed coordinates
  piece.transform.Translate(new Vector3(x,y,200));
   
  // capture original position
  piece.AddComponent();
  PositionData storage = piece.GetComponent();
  storage.OriginalPosition = piece.transform.position;
  
  var tempMaterial = new Material(piece.renderer.material); // 
  tempMaterial.mainTexture = Picture;
  piece.renderer.material = tempMaterial;


  //plane.renderer.sharedMaterial.mainTexture = Picture;
  piece.transform.parent = GameObject.Find("scene").transform;
  return piece;
 
 }
 
 void Shuffle() {
  Helper.ShuffleGameObjects();
  
 }
}


Helper class
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using System.Linq;

public static class Helper  {

 public static IEnumerable Shuffle(this IEnumerable enumerable)
 {
     var r = new System.Random();
     return enumerable.OrderBy(x=>r.Next()).ToList();
 }
 
 public static IEnumerable IteratePieces() {
  var parent = GameObject.Find("scene").transform;
  for (int i = 0; i < parent.childCount; ++i)
        {
            var piece = parent.GetChild(i).gameObject;
   yield return piece;
        }
 }
 
 public static IEnumerable GetPositions(this IEnumerable enumerable) {
  return enumerable.Select (o=>o.transform.position);
 }
 
 public static string ToString(this IEnumerable enumerable) {
  return string.Join (" ",enumerable.Select (i=>i.ToString()).ToArray());
 }
 
 public static void ShuffleGameObjects() {
  var objects = Helper.IteratePieces().ToList();
  var positions = objects.GetPositions();
  var positionsShuffed = positions.Shuffle().ToList();
  for (int i = 0; i < objects.Count; i++) {
   objects[i].transform.position = positionsShuffed[i];
   PositionData storage = objects[i].GetComponent();
   storage.ShuffledPosition = positionsShuffed[i];
  }
 }
 
 
}

воскресенье, 3 ноября 2013 г.

Console Tetris game


This is a sample implementation of the tetris game in console. The tetris game was implemented both in C++ and C#. C++ version uses a predefined base class that provides access to character console buffer, keyboard control and main generic game cycle. C# version uses the same C++ base class, but this time compiled into managed DLL assembly.

Download C++ console tetris game source code
Download C# console tetris game source code


C++ code sample
const float tetris::SPEED = 0.5; // default seconds per each down move 
const float tetris::HIGH_SPEED = 0.05;

tetris::tetris() : BaseApp(24, 30)
{
 srand (time(NULL));
 pausing = true;
 figure=NULL;
 figureNext=NULL;
 moveTimer =  0;

 DrawScene(); // draw scene
 DrawWalls();


 SpawnNewFigure();

}

void tetris::SpawnNewFigure() 
{
 Figure::POINT pt;
 currentSpeed = SPEED;
 moveDownCounter = 0;
 // take next figure as current one
 if (figure) {
  delete figure;
  figure=NULL;
 }
 if (figureNext) 
  figure = figureNext;

 // initialize current figure for the first time
 // place current figure at initial position on top
 pt.x = (X_SIZE-6)/2;
 pt.y = 2;
 if (!figure) figure = new Figure(pt,this);
 else figure->PlaceTo(pt); 
 figure->Draw();


 // prepare next figure for future cycle
 // create and show next figure on the right
 pt.x = X_SIZE-5;
 figureNext = new Figure(pt,this);
 figureNext->Draw();
 pausing = false;

}

void tetris::KeyPressed(int btnCode)
{
 try {
  if (btnCode == 224) btnCode = getch();
  switch(btnCode) 
  {
   case 119: case 72: currentSpeed=SPEED; break;   // w, up arrow
   case 115: case 80: currentSpeed=HIGH_SPEED; break;  // s, down arrow
   case 97: case 75: figure->MoveLeft(); break;   // a, left arrow
   case 100: case 77: figure->MoveRight(); break;   // d, right arrow
   case 32: figure->Rotate(); break;      // space
   case 'p': pausing=!pausing; break;      // pause
  }
 } catch(...) {
  pausing = true;
  SpawnNewFigure();
 }

}

void tetris::UpdateF(float deltaTime)
{
 try {
  if (figure && !pausing) 
  {
   moveTimer += deltaTime;
   if (moveTimer>=currentSpeed) 
   {
    moveTimer = 0;
    bool ok = figure->MoveDown();
    if (ok) 
    {
     moveDownCounter++;
     ok = figure->CanMoveDown();
    }
    if (!ok) 
    {
     pausing = true;
     if (moveDownCounter==0) // if we could not make even 1 move down - GAME OVER: restart a game
     {
      DrawScene(); // draw scene
      DrawWalls();
     }
     SpawnNewFigure(); // if reached a floor, respawn a new figure from top
    }
   }
  }
 } catch(...) {
  pausing = true;
  SpawnNewFigure();
 }
}

void tetris::DrawWalls()
{
 for (int x = 0; x < X_SIZE; x++)
 {
  SetChar(x, 0, W);
  SetChar(x, Y_SIZE-1, W);
 }

 for (int y = 0; y < Y_SIZE; y++)
 {
  SetChar(0, y, W);
  SetChar(X_SIZE-1, y, W);
 }

 for (int x = X_SIZE-7; x < X_SIZE; x++)
 {
  SetChar(x, 5, W);
 }

 for (int y = 0; y < 5; y++)
 {
  SetChar(X_SIZE-7, y, W);
 }

}

void tetris::DrawScene() 
{
 for (int x = 0; x < X_SIZE; x++)
 {
  for (int y = 0; y < Y_SIZE; y++)
  {
   SetChar(x,y, E);
  }
 }
}

C# source code sample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

using FigureLayout = System.Collections.Generic.List;

namespace tetrisCSharp
{
    
    class Tetris : BaseApp
    {
        public const char W = '#'; // wall character
        public const char F = '*'; // figure character
        public const char E = '.'; // empty character

        float moveTimer;
        bool pausing;
        int moveDownCounter;

        const float SPEED = 0.5f; // default speed: seconds per move 
        const float HIGH_SPEED = 0.05f; // high speed

        float currentSpeed = SPEED;   // current speed

        Figure figure;
        Figure figureNext;

        void DrawWalls()
        {
            Enumerable.Range(0, X_SIZE).ForEach(x => { SetChar(x, 0, W); SetChar(x, Y_SIZE - 1, W); }); // horizontal walls
            Enumerable.Range(0, Y_SIZE).ForEach(y => { SetChar(0, y, W); SetChar(X_SIZE - 1, y, W); }); // vertical walls

            // box, showing next figure
            Enumerable.Range(X_SIZE - 7, 7).ForEach(x => SetChar(x, 5, W));
            Enumerable.Range(0, 5).ForEach(y => SetChar(X_SIZE - 7, y, W));
        }
        
        void DrawScene()
        {
            // place empty char in all cells
            Enumerable.Range(0, X_SIZE).CrossJoinAndExecute(Enumerable.Range(0, Y_SIZE), (x, y) => SetChar(x, y, E));
        }

        void SpawnNewFigure()
        {
            POINT pt;
            currentSpeed = SPEED;
            moveDownCounter = 0;
            // take next figure as current one
            if (figure != null)
            {
                figure = null;
            }
            if (figureNext != null)
                figure = figureNext;

            // initialize current figure for the first time
            // place current figure at initial position on top
            pt.x = (X_SIZE - 6) / 2;
            pt.y = 2;
            if (figure == null) figure = new Figure(pt, this);
            else figure.PlaceTo(pt);
            figure.Draw();


            // prepare next figure for future cycle
            // create and show next figure on the right
            pt.x = X_SIZE - 5;
            figureNext = new Figure(pt, this);
            figureNext.Draw();
            pausing = false;
        }

        internal Tetris()
            : base(24, 30)
        {
            pausing = true;
            moveTimer = 0;

            DrawScene(); // draw scene
            DrawWalls();


            SpawnNewFigure();
        }

       
        public override void KeyPressed(int btnCode)
        {
            try
            {
                switch (btnCode)
                {
                    case 119:
                    case 72: currentSpeed = SPEED; break;   // w, up arrow
                    case 115:
                    case 80: currentSpeed = HIGH_SPEED; break;  // s, down arrow
                    case 97:
                    case 75: figure.MoveLeft(); break;       // a, left arrow
                    case 100:
                    case 77: figure.MoveRight(); break;       // d, right arrow
                    case 32: figure.Rotate(); break;    // space
                    case 'p': pausing = !pausing; break;   // pause
                }
            }
            catch
            {
                pausing = true;
                SpawnNewFigure();
            }
        }

        public override void UpdateF(float deltaTime)
        {
            try
            {
                if (figure!=null && !pausing)
                {
                    moveTimer += deltaTime;
                    if (moveTimer >= currentSpeed)
                    {
                        moveTimer = 0;
                        bool ok = figure.MoveDown();
                        if (ok)
                        {
                            moveDownCounter++;
                            ok = figure.CanMoveDown();
                        }
                        if (!ok)
                        {
                            pausing = true;
                            if (moveDownCounter == 0) // if we could not make even 1 move down - GAME OVER: restart a game
                            {
                                DrawScene(); // draw scene
                                DrawWalls();
                            }
                            SpawnNewFigure(); // if reached a floor, respawn a new figure from top
                        }
                    }
                }
            }
            catch
            {
                pausing = true;
                SpawnNewFigure();
            }
        }
    }
}


среда, 30 октября 2013 г.

Sample Unity3D snake game

Play snake game online
Mouse position is captured to drive the snake. SmoothFollowTarget script is used to follow a captured mouse position, as well as for the tail to follow ancestor cell and a head of the snake.
Check out the Unity3D project files from here: https://dl.dropboxusercontent.com/u/50151183/blog/unity/SnakeSample/SnakeSample.zip 

Code:
using UnityEngine;
using System.Collections;

/// 
/// Smooth follow target
/// Used to follow a mouse position as well as for tyhe tail to follow its ancestor
/// 
[RequireComponent(typeof(CharacterController))]
public class SmoothFollowTarget : MonoBehaviour {

    public Transform target;
    public float distance=3;
    public float speed = 40;
    //public float rotationDamping=0.2f;

    private CharacterController controller;


 // Use this for initialization
 void Awake () {
        controller = GetComponent<CharacterController>()
}
 
 // Update is called once per frame
 void Update () {
        //return;
        // Early out if we don't have a target
        if (!target) return;
        // Always look at the target
        transform.LookAt(target);

        // Set the position of the camera on the x-z plane to:
        // distance meters behind the target
        var newPos = target.position - transform.forward * distance;
        newPos.y =1; 
        var speedVector = newPos - transform.position;
        speedVector.y = 0;
        if (speedVector.magnitude>distance)
            controller.SimpleMove(speedVector.normalized * speed);

 }
}

понедельник, 28 октября 2013 г.

coding game contest: Snakes and ladders

Task
Snakes and Ladders (or chutes and ladders) is a board game wherein a player moves from square to square along the board in order to reach the end. Each square corresponds to a particular action which either gives the player an advantage or disadvantage. Each time a player takes his turn, he carries out an action depending on the square on which they land (throw the dice to move, move forward, move back, etc).

The objective of this program is to find out how many turns it takes for the luckiest player to win the game.

BOARD:
We consider a version of the game where the board is composed of a series of squares. Each square can contain either:
  • The character S which represents the starting square
  • The character E which represents the finishing square
  • A number representing how many squares a player should move forward (a positive number) or move back (a negative number)
  • The character R which indicates that the player should throw the dice to move
The start and finish squares are not necessarily situated at the start and end of the board.

GAME:
At the start of the game, the player begins on the starting square (labeled S). They start by throwing the dice and moving the number of squares indicated on the dice. They then carry out the action which is indicated on the square. Each action takes one turn (throw the dice to move, move forward, move back). The game is won when a player reaches the finishing square (labeled E).

If we were to have the following board :
  S  1  R  4  3  4  3  -5  2  -4  E

The fastest way to win in this case is to throw a 2 on the first throw of the dice, which will bring the player to the square R. Then they will throw the dice again and get 6, so they will move forward and land on a square with 2. On the last turn, they will not have a choice: they will move forward two squares and arrive on the square E.

The minimum number of turns to win this game is therefore 3.

Write a program that finds the minimum number of turns given the specified board.

In case a player cannot reach the finishing square, the program should displayimpossible.
INPUT:
Line 1: the number N of squares on the board
N following lines:  one square value per line (RSE or a number)
OUTPUT:
The minimum number of turns required to reach the finishing square or impossible.
CONSTRAINTS:
0 < N < 5000
-N < number value of a square < N

The dice has 6 sides: values are 1, 2, 3, 4, 5, 6.
S and E are unique and always exist.
EXAMPLE:
Input
11
S
1
R
4
3
4
3
-5
2
-4
E
Output
3



 class Task2
    {
        public int N { get; set; }
        List board = new List();

       
        public void Run()
        {
            using (new ConsoleManager("Test_2_input.txt", "Test_2_outputMy.txt"))
            {
                GetData();

                int steps = Go(board, 0);
                Console.WriteLine(steps);

            }
        }

        int Go(List board, int steps)
        {
            Debug.WriteLine("Step {0}: {1}", steps, string.Join(",", board));
            int val;
            Predicate isNumber = s => int.TryParse(s, out val);
            bool boardUpdated = false;
            for (int i = 0; i < board.Count; i++)
            {
                var cell = board[i];
                if (cell == "S" && board.GetRange(i + 1, 6).Contains("E"))
                    return steps + 1;
                else if (cell == "R" && board.GetRange(i + 1, 6).Contains("E"))
                { 
                    board[i] = "E"; 
                    boardUpdated = true; 
                }
                else if (isNumber(cell) && board[i + int.Parse(cell)] == "E")
                { 
                    board[i] = "E"; 
                    boardUpdated = true; 
                }
            }
            if (!boardUpdated) return 0;
            int stepsFinal = Go(board, steps + 1);
            return stepsFinal;
        }

        void GetData()
        {
            string line = Console.ReadLine();
            N = Convert.ToInt32(line);
            for (int i = 0; i < N; i++)
            {
                line = Console.ReadLine();
                board.Add(line);
            }
            Debug.Print("N =" + N + "; " + board.Print());
        }
    }


codingame contest: "Scrabble" task

Task
When playing Scrabble©, each player draws 7 letters and must find a word that scores the most points using these letters.

A player doesn't necessarily have to make a 7-letter word; the word can be shorter. The only constraint is that the word must be made using the 7 letters which the player has drawn.

For example, with the letters  etaenhs, some possible words are: ethanehates,saneant.

LETTER SCORING:
In Scrabble©, each letter is weighted with a score depending on how difficult it is to place that letter in a word. You will see below a table showing the points corresponding to each letter:
LettersPoints
      e, a, i, o, n, r, t, l, s, u   1
      d, g   2
      b, c, m, p   3
      f, h, v, w, y   4
      k   5
      j, x   8
      q, z   10
The word banjo earns 3 + 1 + 1 + 8 + 1 = 14 points.

A dictionary of authorized words is provided as input for the program. The program must find the word in the dictionary which wins the most points for the seven given letters. If two words win the same number of points, then the word which appears first in the order of the given dictionary should be chosen.
All words will only be composed of alphabetical characters in lower case. There will always be at least one possible word.
INPUT:
Line 1: The number N of words in the dictionary
N following lines: The words in the dictionary. One word per line.
Last line: The 7 letters available.
OUTPUT:
The word that scores the most points using the available letters (1 to 7 letters). The word must belong to the dictionnary. There is always a solution.
CONSTRAINTS:
0 < N < 100000
Words in the dictionary have a maximum length of 30 characters.
EXAMPLE:
Input
5
because
first
these
could
which
hicquwh
Output
which



   class Task1
    {
        public int N { get; set; }
        List board = new List();
        public string Letters { get; set; }

        public void Run()
        {
            using (new ConsoleManager("Test_1_input.txt", "Test_1_outputMy.txt"))
            {
                GetData();
                var filtered = board.Where(w => FromLetters(w, Letters.ToCharArray())).ToList();
                var scores = filtered.Select(w => Score(w)).ToList();
                var max = scores.Max();
                var index = scores.FindIndex(score=>score==max);
                if (index >= 0)
                {
                    var word = filtered[index];
                    Console.WriteLine(word);
                }
            }
        }

        bool FromLetters(string word, char[] letters)
        {
            bool yes = true;
            var list = letters.ToList();
            for (int i = 0; i < word.Length; i++)
            {
                var index = list.FindIndex(w=>w==word[i]);
                if (index < 0) { yes = false; break; }
                list.RemoveAt(index);
            }

            
            return yes;
        }

        int Score(string word)
        {
            int score = 0;
            var arr = new String(word.ToCharArray().Distinct().ToArray());
            for (int i = 0; i < arr.Length; i++)
            {
                if ("eaionrtlsu".Contains(word[i])) score++;
                else if ("dg".Contains(word[i])) score+=2;
                else if ("bcmp".Contains(word[i])) score += 3;
                else if ("fhvwy".Contains(word[i])) score += 4;
                else if ("k".Contains(word[i])) score += 5;
                else if ("jx".Contains(word[i])) score += 8;
                else if ("qz".Contains(word[i])) score += 10; 
                 
            }
            return score;
        }

        void GetData()
        {
            string line = Console.ReadLine();
            N = Convert.ToInt32(line);
            for (int i = 0; i < N; i++)
            {
                line = Console.ReadLine();
                board.Add(line);
            }
            Letters = Console.ReadLine();
            Debug.Print("N =" + N + "; " + board.Print());
            Debug.Print("Letters =" + N + "; " + Letters);
        }
    }


воскресенье, 27 октября 2013 г.

Test


public static void Run()
        {
            List numbers = new List();
            string line = Console.ReadLine();
            //int item = 0;
            int n = Convert.ToInt32(line);
            if (n > 0)
            {
                for (int i = 0; i < n; i++)
                {
                    line = Console.ReadLine();
                    numbers.Add(line);
                }
            }

            BuildTree(numbers.Distinct().ToList());

            Console.WriteLine(counter);
        }