Monday, 30 June 2014
LINQ
Populating data-rows into a collection list (Class)
public class TaskStep
{
public int stepId {get; set;}
public string stepName {get; set;}
public string connectionString {get; set;}
}
-------
List<TaskStep> taskSteps = steps.Rows
.Cast<DataRow>()
.Select(row => new MyDomain.Client.Data.TaskStep
{
stepId = (int)row[0],
stepName = (string)row[1],
connectionString = (string)row[2]
})
.ToList();
Wednesday, 18 June 2014
Tic Tac Toe
---------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication3.Models
{
public class TicTacToe
{
public char[] ticBoard;
public int Player = 1; // player 1 is X; player 2
public static int turns { get; set; }
public TicTacToe()
{
ticBoard = new char[10];
for (int iSquare = 1; iSquare <= 9; iSquare++) //program wil ignor the 0 index
{
//Player = 2;
ticBoard[iSquare] = '-'; //empty char
}
}
public void makeATic()
{
//// for winning for testing
//if (turns == 0)
//{
// ticBoard[1] = 'O';
// ticBoard[2] = 'O';
// ticBoard[3] = 'O';
// ticBoard[4] = 'X';
// ticBoard[8] = 'X';
// ticBoard[9] = 'X';
// turns = 5;
//}
turns++;
// bool validPos = false;
Random _r = new Random();
//int iPos = _r.Next(1, 10); // no ze index
List<char> listTicBoard = new List<char>();
listTicBoard = ticBoard.ToList();
int iSquare = listTicBoard.IndexOf('-');
do // (int iSquare = listTicBoard.IndexOf('-'); iSquare <= 9; iSquare++) //program wil ignor the 0 index
{
if (ticBoard[iSquare] == '-') // this square is yet to play
{
//bool playThis = _r.Next(0, 1) == 0 ? false : true; // you wanna play this
if (((_r.Next(1, 100) % 2) == 1) || (turns == 9)) // if odd then play
{
ticBoard[iSquare] = Player == 1 ? 'X' : 'O'; //place the tick
Player = Player == 1 ? 2 : 1; //switch the player
break; //played
}
}
iSquare = iSquare < 9 ? iSquare + 1 : iSquare;
iSquare = listTicBoard.IndexOf('-', iSquare);
} while (iSquare != -1); // all being palyed
}
public int doWeHaveAWinner()
{
if (hasAWinningCombination() == false && turns <= 9)
{
return 0; //draw
}
else
return Player == 1 ? 2 : 1; //here is the winnder
}
public bool hasAWinningCombination()
{
if (turns <= 4) // cannot have a winner
return false;
List<string> winningCombination = new List<string>();
winningCombination.Add("123");
winningCombination.Add("456");
winningCombination.Add("789");
winningCombination.Add("147");
winningCombination.Add("258");
winningCombination.Add("369");
winningCombination.Add("357");
winningCombination.Add("159");
for (int iCounter = 0; iCounter <= winningCombination.Count - 1; iCounter++)
{
bool hasAWinner = true;
List<char> t = winningCombination[0].ToList();
foreach (char pos in t)
{
char PlayerChar = Player == 2 ? 'X' : 'O'; //player is switched at this point
if (ticBoard[Convert.ToInt16(pos.ToString())] != PlayerChar)
{
hasAWinner = false; //not this combination
break;
}
}
if (hasAWinner == true)
return true;
}
return false;
}
}
}
-----------------------------------
--Home Controller About Action Method
--------------------------------
public ActionResult About()
{
TicTacToe ticboard = new TicTacToe();
int hasAWinner = 0;
TicTacToe.turns = 0;
ViewBag.WinningPlayer = ", OOPS It's a draw";
while (hasAWinner == 0 && TicTacToe.turns < 9)
{
ticboard.makeATic();
if (TicTacToe.turns > 4) // cannot have a winner
hasAWinner = ticboard.doWeHaveAWinner();
if (hasAWinner != 0)
{
ViewBag.WinningPlayer = "Player " + hasAWinner;
break;
}
}
var Lines = ticboard.ticBoard;
foreach(char n in Lines)
{
break;
}
ViewBag.Line1 = ticboard.ticBoard[1].ToString() + ticboard.ticBoard[2].ToString() + ticboard.ticBoard[3].ToString();
ViewBag.Line2 = ticboard.ticBoard[4].ToString() + ticboard.ticBoard[5].ToString() + ticboard.ticBoard[6].ToString();
ViewBag.Line3 = ticboard.ticBoard[7].ToString() + ticboard.ticBoard[8].ToString() + ticboard.ticBoard[9].ToString();
ViewBag.Message = "Play tic tac toe.";
return View();
}
-------------------------------------------
--About.chtml
------------------------------------------
@{
ViewBag.Title = "Tic Tac Toe";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
<article>
<p> @ViewBag.Line1</p>
<p> @ViewBag.Line2</p>
<p> @ViewBag.Line3</p>
<p><b>Winner is @ViewBag.WinningPlayer</b></p>
</article>
<aside>
<h3>Aside Title</h3>
<p>
Use this area to provide additional information.
</p>
<ul>
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</aside>
Subscribe to:
Posts (Atom)