Skip to content

Instantly share code, notes, and snippets.

@laoshanlungo
Created July 4, 2018 22:37
Show Gist options
  • Save laoshanlungo/88d0563bf0e7a8fc3880a511d4be462f to your computer and use it in GitHub Desktop.
Save laoshanlungo/88d0563bf0e7a8fc3880a511d4be462f to your computer and use it in GitHub Desktop.
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using ilf.pgn;
using System.Xml;
//using System.Windows.Shapes;
namespace Prog3SchachGui
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public string test;
string[] moves;
int counter;
Image[,] tiles = new Image[8, 8];
RotateTransform rt = new RotateTransform(180);
RotateTransform reset = new RotateTransform(0);
ScaleTransform flip = new ScaleTransform(-1, 0);
public void setSquare()
{
}
public MainWindow()
{
BitmapImage white;
BitmapImage black;
BitmapImage queenW;
BitmapImage pawnW;
BitmapImage rookW;
BitmapImage knightW;
BitmapImage bishopW;
BitmapImage kingW;
BitmapImage queenB;
BitmapImage pawnB;
BitmapImage rookB;
BitmapImage knightB;
BitmapImage bishopB;
BitmapImage kingB;
InitializeComponent();
for (byte i = 0; i<8; i++)
{
ColumnDefinition col = new ColumnDefinition();
col.Width = new GridLength(54, GridUnitType.Pixel);
RowDefinition row = new RowDefinition();
//row.Height = new GridLength(1 / 8, GridUnitType.Star);
row.Height = new GridLength(54, GridUnitType.Pixel);
chessBoard.ColumnDefinitions.Add(col);
chessBoard.RowDefinitions.Add(row);
}
//Load image files
pawnW = new BitmapImage();
pawnW.BeginInit();
pawnW.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "pawnW.png"), UriKind.Relative);
pawnW.DecodePixelHeight = 54;
pawnW.DecodePixelWidth = 54;
pawnW.CacheOption = BitmapCacheOption.OnLoad;
pawnW.EndInit();
rookW = new BitmapImage();
rookW.BeginInit();
rookW.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "rookW.png"), UriKind.Relative);
rookW.DecodePixelHeight = 54;
rookW.DecodePixelWidth = 54;
rookW.CacheOption = BitmapCacheOption.OnLoad;
rookW.EndInit();
knightW = new BitmapImage();
knightW.BeginInit();
knightW.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "knightW.png"), UriKind.Relative);
knightW.DecodePixelHeight = 54;
knightW.DecodePixelWidth = 54;
knightW.CacheOption = BitmapCacheOption.OnLoad;
knightW.EndInit();
bishopW = new BitmapImage();
bishopW.BeginInit();
bishopW.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "bishopW.png"), UriKind.Relative);
bishopW.DecodePixelHeight = 54;
bishopW.DecodePixelWidth = 54;
bishopW.CacheOption = BitmapCacheOption.OnLoad;
bishopW.EndInit();
kingW = new BitmapImage();
kingW.BeginInit();
kingW.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "kingW.png"), UriKind.Relative);
kingW.DecodePixelHeight = 54;
kingW.DecodePixelWidth = 54;
kingW.CacheOption = BitmapCacheOption.OnLoad;
kingW.EndInit();
queenW = new BitmapImage();
queenW.BeginInit();
queenW.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "queenW.png"), UriKind.Relative);
queenW.DecodePixelHeight = 54;
queenW.DecodePixelWidth = 54;
queenW.CacheOption = BitmapCacheOption.OnLoad;
queenW.EndInit();
pawnB = new BitmapImage();
pawnB.BeginInit();
pawnB.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "pawnB.png"), UriKind.Relative);
pawnB.DecodePixelHeight = 54;
pawnB.DecodePixelWidth = 54;
pawnB.CacheOption = BitmapCacheOption.OnLoad;
pawnB.EndInit();
rookB = new BitmapImage();
rookB.BeginInit();
rookB.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "rookB.png"), UriKind.Relative);
rookB.DecodePixelHeight = 54;
rookB.DecodePixelWidth = 54;
rookB.CacheOption = BitmapCacheOption.OnLoad;
rookB.EndInit();
knightB = new BitmapImage();
knightB.BeginInit();
knightB.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "knightB.png"), UriKind.Relative);
knightB.DecodePixelHeight = 54;
knightB.DecodePixelWidth = 54;
knightB.CacheOption = BitmapCacheOption.OnLoad;
knightB.EndInit();
bishopB = new BitmapImage();
bishopB.BeginInit();
bishopB.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "bishopB.png"), UriKind.Relative);
bishopB.DecodePixelHeight = 54;
bishopB.DecodePixelWidth = 54;
bishopB.CacheOption = BitmapCacheOption.OnLoad;
bishopB.EndInit();
queenB = new BitmapImage();
queenB.BeginInit();
queenB.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "queenB.png"), UriKind.Relative);
queenB.DecodePixelHeight = 54;
queenB.DecodePixelWidth = 54;
queenB.CacheOption = BitmapCacheOption.OnLoad;
queenB.EndInit();
kingB = new BitmapImage();
kingB.BeginInit();
kingB.UriSource = new Uri(Path.Combine(Environment.CurrentDirectory, @"components\images", "kingB.png"), UriKind.Relative);
kingB.DecodePixelHeight = 54;
kingB.DecodePixelWidth = 54;
kingB.CacheOption = BitmapCacheOption.OnLoad;
kingB.EndInit();
//chessBoard.Background = new SolidColorBrush(Color.FromArgb(100, 244, 164, 90));
//Generate tiles as image nodes
for (byte x = 0;
x < 8; x++)
{
for (byte y = 0; y < 8; y++)
{
tiles[x, y] = new Image();
tiles[x, y].HorizontalAlignment = HorizontalAlignment.Stretch;
tiles[x, y].VerticalAlignment = VerticalAlignment.Stretch;
tiles[x, y].Cursor = Cursors.Cross;
tiles[x, y].Height = 50;
tiles[x, y].Width = 50;
tiles[x, y].Focusable = true;
if(y == 1)
{
tiles[x, y].Source = pawnW;
}
if (y == 6)
{
tiles[x, y].Source = pawnB;
}
if(y == 0)
{
if(x == 0 | x == 7)
{
tiles[x, y].Source = rookW;
}
if (x == 1 | x == 6)
{
tiles[x, y].Source = knightW;
}
if (x == 2 | x == 5)
{
tiles[x, y].Source = bishopW;
}
if (x == 3)
{
tiles[x, y].Source = queenW;
}
if (x == 4)
{
tiles[x, y].Source = kingW;
}
}
if (y == 7)
{
if (x == 0 | x == 7)
{
tiles[x, y].Source = rookB;
}
if (x == 1 | x == 6)
{
tiles[x, y].Source = knightB;
}
if (x == 2 | x == 5)
{
tiles[x, y].Source = bishopB;
}
if (x == 3)
{
tiles[x, y].Source = queenB;
}
if(x == 4)
{
tiles[x, y].Source = kingB;
}
}
tiles[x, y].Visibility = System.Windows.Visibility.Visible;
tiles[x, y].LayoutTransform = rt;
this.chessBoard.Children.Add(tiles[x, y]);
Grid.SetRow(tiles[x, y], y);
Grid.SetColumn(tiles[x, y], x);
}
}
chessBoard.LayoutTransform = rt;
}
private void Save_Click(object sender, RoutedEventArgs e)
{
// MessageBox.Show("File saved!");
SaveFileDialog dlg = new SaveFileDialog();
var r = dlg.ShowDialog();
if (!(bool)r)
{
return;
}
StreamWriter sw = new StreamWriter(dlg.FileName);
sw.Write(AusgabeBox);
AusgabeBox.Text = "File saved under " + dlg.FileName;
sw.Close();
}
private void Load_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.CheckFileExists = true;
var r = dlg.ShowDialog();
if (!(bool)r)
{
return;
}
string a = dlg.FileName;
string test = " ";
XmlDocument xmlDoc = new XmlDocument();
try {
xmlDoc.Load(a);
}
catch(FileNotFoundException i)
{
MessageBox.Show("file not found" + i);
}
catch(Exception u)
{
MessageBox.Show("unexpected exception" + u);
}
string xpath = "Partie";
var nodes = xmlDoc.SelectNodes(xpath);
foreach (XmlNode childrenNode in nodes)
{
test = test + childrenNode.SelectSingleNode("//zuege").InnerText;
}
moves = test.Split(default(Char[]), StringSplitOptions.RemoveEmptyEntries);
MessageBox.Show("Game Loaded successfully");
counter = 0;
//using (XmlReader reader = XmlReader.Create(dlg.FileName))
//{
// reader.MoveToAttribute("zuege");
// do
// {
// switch (reader.NodeType)
// {
// case XmlNodeType.Element:
// Console.Write("<{0}", reader.Name);
// while (reader.MoveToNextAttribute())
// {
// Console.Write("asd {0}='{1}'", reader.Name, reader.Value);
// }
// Console.Write(">");
// break;
// case XmlNodeType.Text:
// Console.Write(reader.Value);
// break;
// case XmlNodeType.EndElement:
// Console.Write("</{0}>", reader.Name);
// break;
// }
// } while (reader.Read());
//}
//var xmlDoc = new XmlDocument();
//StreamReader xmlStream = new StreamReader(dlg.FileName);
//xmlDoc.Save(xmlStream);
//xmlDoc.
//StreamReader sr = new StreamReader(dlg.FileName);
//sr.Close();
//var reader = new PgnReader();
//var db = reader.ReadFromFile(dlg.FileName);
//var game = db.Games[0];
//var moves = game.MoveText;
// string[] tokens = (moves[0].ToString().Split(' '));
// AusgabeBox.Text += moves[0];
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void ShowMoves_Checked(object sender, RoutedEventArgs e)
{
AusgabeBox.Visibility = System.Windows.Visibility.Visible;
}
private void ShowMoves_Unchecked(object sender, RoutedEventArgs e)
{
AusgabeBox.Visibility = System.Windows.Visibility.Hidden;
}
private void Forward_Click(object sender, RoutedEventArgs e)
{
if(moves[counter] == "0-0")
{
MessageBox.Show("lol");
}
else
{
UpdateBoard(moves[counter]);
}
counter = counter + 1;
}
private void Visual_Board_Initialized(object sender, EventArgs e)
{
}
private void UpdateBoard(string move)
{
//this.tiles[((int)move[0] - 97), move[1]] = null;
//if(((move[0] - '0') -48) == 0)
//{
// MessageBox.Show("lol");
//}
byte oldX = Convert.ToByte(move[0]-97);
byte oldY = Convert.ToByte(move[1]-49);
byte newX = Convert.ToByte(move[3] -97);
byte newY = Convert.ToByte(move[4] - 49);
this.tiles[newX, newY].Source = this.tiles[oldX, oldY].Source;
this.tiles[oldX, oldY].Source = null;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
for (byte x = 0; x < 8; x++)
{
for (byte y = 0; y < 8; y++)
{
tiles[x, y].LayoutTransform = reset;
}
}
chessBoard.LayoutTransform = reset;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
for (byte x = 0; x < 8; x++)
{
for (byte y = 0; y < 8; y++)
{
tiles[x, y].LayoutTransform = rt;
}
}
chessBoard.LayoutTransform = rt;
}
private void Backward_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < counter-; i++)
{
if (moves[i] == "0-0")
{
MessageBox.Show("lol");
}
else
{
UpdateBoard(moves[i]);
}
}
}
private void End_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i < moves.Length; i++)
{
if (moves[i] == "0-0")
{
MessageBox.Show("lol");
}
else
{
UpdateBoard(moves[i]);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment