morpionReseau/go01/Plateau.cs

195 lines
6.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace go01
{
public partial class Plateau : Form
{
TextWriter _writer = null;
StreamWriter _writerConsole = null;
enum CouleurVoulue { Red, Green, Blue, Black, LAST }
CouleurVoulue[,] tableauCouleur = new CouleurVoulue[19,19];
List<PictureBox> pboxList = new List<PictureBox>();
Goban g=null;
socketPlateauBase sck = null;
Goban.ConfigGo_S? cfg=null;
Goban.sauvegarde_S? sauvegarde = null;
int dim; int nbPionAlign; string playerW; string playerB;
public Plateau(Goban.sauvegarde_S s)
{
InitializeComponent();
this.Text = $"Morpion {s.cfg.taille}x{s.cfg.taille}";
sauvegarde = s;
}
public Plateau(int dim, int nbPionAlign, string playerW, string playerB)
{
InitializeComponent();
this.Text = $"Morpion {dim}x{dim}";
this.dim = dim;
this.nbPionAlign = nbPionAlign;
this.playerW = playerW;
this.playerB = playerB;
}
public Plateau(Goban.ConfigGo_S cfg, socketPlateauBase sck) : this(cfg.taille, cfg.pionaAligner, cfg.NomJoeurBlanc, cfg.NomJoeurNoir)
{
this.cfg = cfg;
this.sck = sck;
this.Text = $"Morpion {dim}x{dim} "+sck.SocFlag;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
String[] xy = ((PictureBox)sender).Name.Split(';');
if (++tableauCouleur[int.Parse(xy[0]), int.Parse(xy[1])] == CouleurVoulue.LAST)
tableauCouleur[int.Parse(xy[0]), int.Parse(xy[1])] = CouleurVoulue.Red;
((PictureBox)sender).BackColor = Color.FromName((tableauCouleur[int.Parse(xy[0]),int.Parse(xy[1])]).ToString());
Console.WriteLine(((PictureBox)sender).Name);
}
private void chkBxDbg_CheckedChanged(object sender, EventArgs e)
{
// Settings1.Default.dbg = chkBxDbg.Checked;
Settings1.Default.Save();
}
private void txtBoxTmpAnim_TextChanged(object sender, EventArgs e)
{
int tmp;
//Settings1.Default.vitesseAnimDBG = int.TryParse(txtBoxTmpAnim.Text, out tmp) ?tmp: Settings1.Default.vitesseAnimDBG;
Settings1.Default.Save();
}
private void Plateau_Load(object sender, EventArgs e)
{
//base.OnLoad(e);
if (sck != null && cfg != null && sck != null)
g = new Goban(this, pictureBox1, cfg.Value, sck);
else
g = new Goban(this, pictureBox1, dim, nbPionAlign, playerW, playerB);
this.DoubleBuffered = true;
}
private void txtConsole_TextChanged(object sender, EventArgs e)
{
// set the current caret position to the end
//txtConsole.SelectionStart = txtConsole.Text.Length;
// scroll it automatically
//txtConsole.ScrollToCaret();
}
private void txtAffConsole_CheckedChanged(object sender, EventArgs e)
{
/*f(txtAffConsole.Checked)
{
_writer = new TextBoxStreamWriter(txtConsole);
Console.SetOut(_writer);
Console.WriteLine("Now redirecting output to the text box");
}
else
{
_writerConsole = new StreamWriter(Console.OpenStandardOutput());
_writerConsole.AutoFlush = true;
Console.SetOut(_writerConsole);
}*/
}
private void button1_Click(object sender, EventArgs e)
{
g.resetGoban();
}
public void pictureBox2WinVisible(bool t)
{
pictureBox2.Visible = t;
}
private void button1_Click_1(object sender, EventArgs e)
{
g.resetGoban();
}
public string lblNoir
{
set { this.lblPtBlanc.Text = value; }
}
public string lblBlanc
{
set { this.LblPtNoir.Text = value; }
}
public bool btnContinuerEnable
{
set
{
this.btnContinuer.Enabled = value;
}
}
private void btnContinuer_Click(object sender, EventArgs e)
{
g.ContinuerAjouer();
}
private void btnEnreg_Click(object sender, EventArgs e)
{
Stream myStream = null;
SaveFileDialog openFileDialog1 = new SaveFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "bin files (*.bin)|*.bin|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
WriteToBinaryFile<Goban.sauvegarde_S>(openFileDialog1.FileName, g.sauvegarder());
}
}
public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false)
{
using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, objectToWrite);
}
}
/// <summary>
/// Reads an object instance from a binary file.
/// </summary>
/// <typeparam name="T">The type of object to read from the binary file.</typeparam>
/// <param name="filePath">The file path to read the object instance from.</param>
/// <returns>Returns a new instance of the object read from the binary file.</returns>
public static T ReadFromBinaryFile<T>(string filePath)
{
using (Stream stream = File.Open(filePath, FileMode.Open))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
return (T)binaryFormatter.Deserialize(stream);
}
}
private void Plateau_Shown(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
}
private void Plateau_Paint(object sender, PaintEventArgs e)
{
}
}
}