I've got a small bit of code I need to convert to C#. However my experience with C# is very very limited and a simple python program like the one I'm trying to convert is beyond my experience in C#. If anyone knows of any converters or is willing to help me out I would greatly appreciate it!

``````import random

size=5
board= [ ["_","_","_","_","_"],
["_","_","_","_","_"],
["_","_","_","_","_"],
["_","_","_","_","_"],
["_","_","_","_","_"] ]

def printBoard():
for row in board:
for square in row:
print square,
print
print

def place(marker):
placed = False
while not placed:
random_row = random.randrange(len(board))
random_col = random.randrange(len(board))
if(board[random_row][random_col]== "_"):
board[random_row][random_col] = marker
placed = True

def setBoard():
place("*")
b_count=0
while b_count<6:
place("X")
b_count=b_count+1

game_on = True
setBoard()

while game_on == True:
printBoard()
row = 0
while row < 5:
col=0
while col < 5:
if board[row][col]==("*"):
r=row
c=col
row=5
col=5
else:
col=col+1
row=row+1

print
move=(raw_input("Choose your next move (l,r,u,d): "))
if move == ("l"):
if (c-1!=-1) and (board[r][c-1]!= ("X")):
board[r][c]=("X")
c=c-1
board[r][c]=("*")
if move == ("r"):
if (c+1!=5) and (board[r][c+1]!= ("X")):
board[r][c]=("X")
c=c+1
board[r][c]=("*")
if move == ("u"):
if (r-1!=-1) and (board[r-1][c]!= ("X")):
board[r][c]=("X")
r=r-1
board[r][c]=("*")
if move == ("d"):
if (r+1!=5) and (board[r+1][c]!= ("X")):
board[r][c]=("X")
r=r+1
board[r][c]=("*")

if board == "*":
printBoard()
game_on = False
print("Congratulations you won!")
elif board == "X":
game_on = False
print("Sorry, there is no way to win. Please press Enter to restart.")
print
raw_input("Press Enter to exit")``````

## All 2 Replies

The closest thing I know to solve your problem is the Boo language. It uses pretty much Python syntax but compiles with the Microsoft.NET C# compiler. Take a look at:
http://www.daniweb.com/code/snippet429.html

In deference to your inexperience, I tried to match the C# as closely as possible to the original Python, so it won't be hard to follow. Note that program execution starts in Main() function, but most of the real work happens in Run().

Changes:
declaring all variables and their types prior to use,
replacing python IO calls with .NET IO calls
keyword translation like 'and' become '&&'

``````using System;
using System.Collections.Generic;

class Program {
char[][] board;
bool placed;
Random r = new Random();

public void printBoard() {
Console.WriteLine();
foreach (char[] row in board) {
foreach (char square in row)
Console.Write(square);
Console.WriteLine();
}
}
public void place(char marker) {
placed = false;
while (!placed) {
int random_row = r.Next(board.Length);
int random_col = r.Next(board.Length);
if (board[random_row][random_col] == '_') {
board[random_row][random_col] = marker;
placed = true;
}
}
}
public void setBoard() {
place('*');
int b_count = 0;
while (b_count < 6) {
place('X');
b_count = b_count + 1;
}
}

public void Run() {
board = new char[][] {
new char[] { '_','_','_','_','_' },
new char[] { '_','_','_','_','_' },
new char[] { '_','_','_','_','_' },
new char[] { '_','_','_','_','_' },
new char[] { '_','_','_','_','_' }};

bool game_on = true;
setBoard();

while (game_on) {
printBoard();
int row = 0;
int r = -1, c = -1;
while (row < 5) {
int col = 0;
while (col < 5) {
if (board[row][col] == '*') {
r = row;
c = col;
row = 5;
col = 5;
}
col = col + 1;
}
row = row + 1;
}
Console.Write("\nChoose your next move (l,r,u,d): ");
switch (move) {
case 'l':
if ((c - 1 != -1) && (board[r][c - 1] != 'X')) {
board[r][c] = 'X';
c = c - 1;
board[r][c] = '*';
}
break;
case 'r':
if ((c + 1 != 5) && (board[r][c + 1] != ('X'))) {
board[r][c] = ('X');
c = c + 1;
board[r][c] = ('*');
}
break;
case 'u':
if ((r - 1 != -1) && (board[r - 1][c] != ('X'))) {
board[r][c] = ('X');
r = r - 1;
board[r][c] = ('*');
}
break;
case 'd':
if ((r + 1 != 5) && (board[r + 1][c] != ('X'))) {
board[r][c] = ('X');
r = r + 1;
board[r][c] = ('*');
}
break;
}
if (board == '*') {
printBoard();
game_on = false;
Console.WriteLine("Congratulations you won!");
} else if (board == 'X') {
game_on = false;
Console.WriteLine("Sorry, there is no way to win. Please press Enter to restart.");
}
}
Console.WriteLine("Press Enter to exit");