working multiplayer

This commit is contained in:
anon 2022-11-28 15:13:54 +01:00
parent 0adfbc9eba
commit a7e814e6f0
8 changed files with 276 additions and 75 deletions

View File

@ -17,7 +17,7 @@ client:
${COMP} ${SRCD}/ClientProgram.cs -out:$(word 3,${OUTPUT})
singleplayer:
${COMP} ${SRCD}/Program.cs -out:$(word 1,${OUTPUT})
${COMP} ${SRCD}/SinglePlayer.cs -out:$(word 1,${OUTPUT})
run:
mono $(word 2,3,${OUTPUT})

View File

@ -6,15 +6,15 @@ public class Asztal{
// Belső változók
//------------------------
public const uint TERMET = 8;
private const string VKOCKA_ELEM = "+---";
private const string HKOCKA_ELEM = "| ";
private const string VKOCKA_ELEM = "+───";
private const string HKOCKA_ELEM = " ";
private const string VKOCKA_DUGASZ = "+";
private const string HKOCKA_DUGASZ = "|";
private const string HKOCKA_DUGASZ = "";
private Jatekmod jatekmod;
private Figura[,] rublika = new Figura[TERMET, TERMET];
private List<Figura> halottak = new List<Figura>();
private Szin mozgatgat = Szin.FEHER;
private Szin _mozgathat = Szin.FEHER;
// ### Set-Get ###
public Jatekmod Jatekmod{
@ -29,7 +29,6 @@ public class Asztal{
set {
if(_x > TERMET || _y > TERMET || _x == 0 || _y == 0) throw new ArgumentOutOfRangeException("");
rublika[_x-1, _y-1] = value;
}
}
public void addHalottak(Figura figura){
@ -38,6 +37,14 @@ public class Asztal{
public void clearHalottak(){
this.halottak.Clear();
}
public void turn_turns(){
if(this._mozgathat == Szin.FEHER){
this._mozgathat = Szin.FEKETE;
}else{
this._mozgathat = Szin.FEHER;
}
}
public Szin mozgathat{ get => _mozgathat; }
//------------------------
// Belső eljárások
@ -79,7 +86,7 @@ public class Asztal{
// létező figura?
if(this[_x1, _y1] == null){ Figura.mozgat_errno = MozgatErrno.NULL_FIGURA; return false; }
// jó szin?
if(this[_x1, _y1].Szin != this.mozgatgat){ Figura.mozgat_errno = MozgatErrno.ROSSZ_SZIN; return false; }
if(this[_x1, _y1].Szin != this.mozgathat){ Figura.mozgat_errno = MozgatErrno.ROSSZ_SZIN; return false; }
// -be határon belül?
if(_x2 > TERMET || _y2 > TERMET || _x2 == 0 || _y2 == 0){
Figura.mozgat_errno = MozgatErrno.NEM_RUBLIKA; return false;
@ -89,84 +96,94 @@ public class Asztal{
return this[_x1, _y1].mozgat(_x2, _y2);
}
public void mozgat(){
INPUT:
Console.Write(": ");
string l = Console.ReadLine();
if(l == "pass"){ goto TURN_TURNS; }
string[] k = l.Split("->");
private static bool _mozgat_parse(string s,
out uint x1, out uint y1,
out uint x2, out uint y2){
string[] k = s.Split("->");
foreach(var i in k){
if(i.Length < 2){ goto INPUT; }
if(i.Length < 2){
x1 = 0;
x2 = 0;
y1 = 0;
y2 = 0;
return false;
}
}
uint x1, y1, x2, y2;
x1 = (uint)(k[0][0] - 96);
x2 = (uint)(k[1][0] - 96);
y1 = (uint)(k[0][1] - 48);
y2 = (uint)(k[1][1] - 48);
//Console.WriteLine("{0}, {1}; {2}, {3}", x1, y1, x2, y2);
if(!this._mozgat(x1, y1, x2, y2)){
return;
Console.WriteLine("{0}, {1}; {2}, {3}", x1, y1, x2, y2);
return true;
}
public bool mozgat(string l){
uint x1, y1, x2, y2;
TURN_TURNS:
if(this.mozgatgat == Szin.FEHER){
this.mozgatgat = Szin.FEKETE;
}else{
this.mozgatgat = Szin.FEHER;
if(_mozgat_parse(l, out x1, out y1, out x2, out y2) &&
this._mozgat(x1, y1, x2, y2)){
this.turn_turns();
return true;
}
return false;
}
// ### Állapot ellenőrzők ###
public void print(){
Console.WriteLine("\n\n");
public override string ToString(){
string r = "";
// --- Tábla és bábúk ---
for(uint i = Asztal.TERMET; i >= 1; i--){
Console.Write(" ");
r += " ";
for(uint h = 1; h <= Asztal.TERMET; h++){
Console.Write(Asztal.VKOCKA_ELEM);
r += Asztal.VKOCKA_ELEM;
}
Console.WriteLine(Asztal.VKOCKA_DUGASZ);
r += Asztal.VKOCKA_DUGASZ + "\n";
Console.Write(" " + i + " ");
r += " " + i + " ";
for(uint h = 1; h <= Asztal.TERMET; h++){
Console.Write(Asztal.HKOCKA_ELEM);
r += Asztal.HKOCKA_ELEM;
if(this[h, i] != null){
Console.Write(this[h, i].Jel);
r += this[h, i].Jel;
}else{
Console.Write(' ');
r += ' ';
}
Console.Write(' ');
r += ' ';
}
Console.WriteLine(Asztal.HKOCKA_DUGASZ);
r += Asztal.HKOCKA_DUGASZ + "\n";
}
Console.Write(" ");
r += " ";
for(uint h = 1; h <= Asztal.TERMET; h++){
Console.Write(Asztal.VKOCKA_ELEM);
r += Asztal.VKOCKA_ELEM;
}
Console.WriteLine(Asztal.VKOCKA_DUGASZ);
Console.Write(" ");
r += Asztal.VKOCKA_DUGASZ + "\n";
r += " ";
for(uint i = 0; i < Asztal.TERMET; i++){
Console.Write(" " + (char)('a'+i) + " " );
r += " " + (char)('a'+i) + " " ;
}
Console.WriteLine();
r += "\n";
// --- Leütött bábúk ---
Console.Write("\x1b[1mLeütött bábúk: \x1b[0m");
r += "\x1b[1mLeütött bábúk: \x1b[0m";
foreach(var i in this.halottak){
Console.Write(" " + i.Jel + ',');
r += " " + i.Jel + ',';
}
Console.WriteLine();
r += "\n";
// --- Lépés állapot ---
Console.Write("\x1b[1mSoron következő játékos: \x1b[0m");
if(this.mozgatgat == Szin.FEHER){
Console.Write("□ ");
r += "\x1b[1mSoron következő játékos: \x1b[0m";
if(this.mozgathat == Szin.FEHER){
r += "□ ";
}else{
Console.Write("■ ");
r += "■ ";
}
Console.WriteLine("\n\x1b[1mUtolsó lépés legalitása: \x1b[0m" + Figura.mozgat_errno);
r += "\n\x1b[1mUtolsó lépés legalitása: \x1b[0m" + Figura.mozgat_errno;
r += "\n";
return r;
}
public void print(){
Console.WriteLine("\n\n");
Console.Write(this.ToString());
}
}

View File

@ -1,5 +1,59 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
class Client{
class Client : TcpClient {
const int port = 14792;
const string localhost = "127.0.0.1";
StreamReader reader = null;
StreamWriter writer = null;
public bool doConnect(){
try{
Console.WriteLine("Connecting...");
Connect(localhost, port);
Console.WriteLine("Connected.");
}catch(Exception e){
Console.WriteLine("Exception occured: {0}", e);
Console.WriteLine("Connection failed.");
return false;
}
return Active;
}
public void Main(){
reader = new StreamReader(GetStream());
writer = new StreamWriter(GetStream());
string s;
while(true){
s = reader.ReadLine();
if(s[0] == "\x11"[0]){
s = s.Substring(1);
//Console.WriteLine("Esc: {0}", s);
switch(s){
case "readline":
Console.Write(": ");
writer.WriteLine(Console.ReadLine());
writer.Flush();
break;
case "spinner":
Console.Write("Waiting for oponent");
Console.Write(". . .");
Console.WriteLine("");
break;
}
}else{
Console.WriteLine(s);
}
if(!Connected){
Console.WriteLine("Connection lost.");
break;
}
}
}
}

View File

@ -1,15 +1,17 @@
using System;
using System.Threading;
using System.Collections.Generic;
class Program{
static Random r = new Random();
static class Program{
static Client client = new Client();
static void Main(string[] args){
Asztal tabla = new Asztal(Jatekmod.klasszikus);
while(true){
tabla.print();
tabla.mozgat();
}
do{
if(client.doConnect()){
client.Main();
}
Thread.Sleep(1000);
}while(true);
}
}

View File

@ -26,8 +26,8 @@ public class AtloInfo{
}
public void Calc(uint x, uint y){
this.xy = (int)x - (int)y;
Console.WriteLine("{0} - {1} -> {2}", x, y, this.xy);
//Console.WriteLine("{0} - {1} -> {2}", x, y, this.xy);
this.yx = (int)y + (int)x;
Console.WriteLine("{0} + {1} -> {2}", y, x, this.yx);
//Console.WriteLine("{0} + {1} -> {2}", y, x, this.yx);
}
}

View File

@ -1,7 +1,134 @@
using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
class Server{
static List<Client> = new list<Client>;
const int port = 14792;
IPAddress localhost = IPAddress.Parse("127.0.0.1");
TcpListener server = null;
List<TcpClient> clients = new List<TcpClient>();
List<Thread> matches = new List<Thread>();
const Object matchMakingLock = null;
bool running = false;
public bool Start(){
Console.WriteLine("Server starting...");
server = new TcpListener(localhost, port);
running = true;
Thread ListnerThread = new Thread(Listen);
ListnerThread.Start();
Console.WriteLine("Listening...");
return true;
}
public bool Stop(){
running = false;
return true;
}
void Listen(){
server.Start();
while(running){
var c = server.AcceptTcpClient();
lock(clients){
clients.Add(c);
}
Console.WriteLine("Client connected. New number of Clients: {0}", clients.Count + (matches.Count*2));
}
}
public void MakeMatch(){
while(true){
if(clients.Count != 0 && clients.Count % 2 == 0){
lock(clients) lock(matches){
if(clients.Count != 0 && clients.Count % 2 == 0){
Console.WriteLine("Making a match...");
var c1 = clients[0];
var c2 = clients[1];
clients.RemoveAt(1);
clients.RemoveAt(0);
matches.Add(new Thread(() => {
Console.WriteLine("Match started.");
Play(c1, c2);
Console.WriteLine("Match finished.");
}));
matches[matches.Count-1].Start();
}
}
}
}
}
internal struct Player{
public TcpClient c;
public StreamReader r;
public StreamWriter w;
public Szin s;
};
bool? Play(TcpClient c1, TcpClient c2){
Player[] players = new Player[2];
players[0].c = c1;
players[0].r = new StreamReader(c1.GetStream());
players[0].w = new StreamWriter(c1.GetStream());
players[0].s = Szin.FEHER;
players[1].c = c2;
players[1].r = new StreamReader(c2.GetStream());
players[1].w = new StreamWriter(c2.GetStream());
players[1].s = Szin.FEKETE;
Asztal asztal = new Asztal(Jatekmod.klasszikus);
string s = "";
Thread tInput = null;
while(true){
try{
foreach(var i in players){
i.w.Write(asztal.ToString());
i.w.Flush();
if(i.s == asztal.mozgathat){
i.w.WriteLine("\x11spinner");
i.w.Flush();
}else{
tInput = new Thread(() => {
i.w.WriteLine("\x11readline");
i.w.Flush();
s = i.r.ReadLine();
asztal.mozgat(s);
});
tInput.Start();
}
}
tInput.Join();
Console.WriteLine("Recieved input: {0}", s);
}catch(Exception e){
Console.WriteLine(e);
break;
}
}
foreach(var i in players){
i.w.WriteLine("poll");
i.w.Flush();
if(i.c.Connected){
lock(clients){
clients.Add(i.c);
}
}
}
return null;
}
}

View File

@ -1,15 +1,15 @@
using System;
using System.Collections.Generic;
class Program{
static Random r = new Random();
static class ServerProgram{
static Server server = new Server();
static void Main(string[] args){
Asztal tabla = new Asztal(Jatekmod.klasszikus);
Byte[] buffer = new Byte[256];
String data = null;
while(true){
tabla.print();
tabla.mozgat();
}
server.Start();
server.MakeMatch();
}
}

View File

@ -12,7 +12,8 @@ using System;
while(true){
tabla.print();
tabla.mozgat();
Console.Write(": ");
tabla.mozgat(Console.ReadLine());
}
}
}