working multiplayer
This commit is contained in:
parent
0adfbc9eba
commit
a7e814e6f0
2
Makefile
2
Makefile
@ -17,7 +17,7 @@ client:
|
|||||||
${COMP} ${SRCD}/ClientProgram.cs -out:$(word 3,${OUTPUT})
|
${COMP} ${SRCD}/ClientProgram.cs -out:$(word 3,${OUTPUT})
|
||||||
|
|
||||||
singleplayer:
|
singleplayer:
|
||||||
${COMP} ${SRCD}/Program.cs -out:$(word 1,${OUTPUT})
|
${COMP} ${SRCD}/SinglePlayer.cs -out:$(word 1,${OUTPUT})
|
||||||
|
|
||||||
run:
|
run:
|
||||||
mono $(word 2,3,${OUTPUT})
|
mono $(word 2,3,${OUTPUT})
|
||||||
|
125
src/Asztal.cs
125
src/Asztal.cs
@ -6,15 +6,15 @@ public class Asztal{
|
|||||||
// Belső változók
|
// Belső változók
|
||||||
//------------------------
|
//------------------------
|
||||||
public const uint TERMET = 8;
|
public const uint TERMET = 8;
|
||||||
private const string VKOCKA_ELEM = "+---";
|
private const string VKOCKA_ELEM = "+───";
|
||||||
private const string HKOCKA_ELEM = "| ";
|
private const string HKOCKA_ELEM = "│ ";
|
||||||
private const string VKOCKA_DUGASZ = "+";
|
private const string VKOCKA_DUGASZ = "+";
|
||||||
private const string HKOCKA_DUGASZ = "|";
|
private const string HKOCKA_DUGASZ = "│";
|
||||||
|
|
||||||
private Jatekmod jatekmod;
|
private Jatekmod jatekmod;
|
||||||
private Figura[,] rublika = new Figura[TERMET, TERMET];
|
private Figura[,] rublika = new Figura[TERMET, TERMET];
|
||||||
private List<Figura> halottak = new List<Figura>();
|
private List<Figura> halottak = new List<Figura>();
|
||||||
private Szin mozgatgat = Szin.FEHER;
|
private Szin _mozgathat = Szin.FEHER;
|
||||||
|
|
||||||
// ### Set-Get ###
|
// ### Set-Get ###
|
||||||
public Jatekmod Jatekmod{
|
public Jatekmod Jatekmod{
|
||||||
@ -27,9 +27,8 @@ public class Asztal{
|
|||||||
return rublika[_x-1, _y-1];
|
return rublika[_x-1, _y-1];
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
if(_x > TERMET || _y > TERMET || _x == 0 || _y == 0) throw new ArgumentOutOfRangeException("");
|
if(_x > TERMET || _y > TERMET || _x == 0 || _y == 0) throw new ArgumentOutOfRangeException("");
|
||||||
rublika[_x-1, _y-1] = value;
|
rublika[_x-1, _y-1] = value;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void addHalottak(Figura figura){
|
public void addHalottak(Figura figura){
|
||||||
@ -38,6 +37,14 @@ public class Asztal{
|
|||||||
public void clearHalottak(){
|
public void clearHalottak(){
|
||||||
this.halottak.Clear();
|
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
|
// Belső eljárások
|
||||||
@ -79,7 +86,7 @@ public class Asztal{
|
|||||||
// létező figura?
|
// létező figura?
|
||||||
if(this[_x1, _y1] == null){ Figura.mozgat_errno = MozgatErrno.NULL_FIGURA; return false; }
|
if(this[_x1, _y1] == null){ Figura.mozgat_errno = MozgatErrno.NULL_FIGURA; return false; }
|
||||||
// jó szin?
|
// 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?
|
// -be határon belül?
|
||||||
if(_x2 > TERMET || _y2 > TERMET || _x2 == 0 || _y2 == 0){
|
if(_x2 > TERMET || _y2 > TERMET || _x2 == 0 || _y2 == 0){
|
||||||
Figura.mozgat_errno = MozgatErrno.NEM_RUBLIKA; return false;
|
Figura.mozgat_errno = MozgatErrno.NEM_RUBLIKA; return false;
|
||||||
@ -89,84 +96,94 @@ public class Asztal{
|
|||||||
|
|
||||||
return this[_x1, _y1].mozgat(_x2, _y2);
|
return this[_x1, _y1].mozgat(_x2, _y2);
|
||||||
}
|
}
|
||||||
public void mozgat(){
|
private static bool _mozgat_parse(string s,
|
||||||
|
out uint x1, out uint y1,
|
||||||
INPUT:
|
out uint x2, out uint y2){
|
||||||
Console.Write(": ");
|
string[] k = s.Split("->");
|
||||||
string l = Console.ReadLine();
|
foreach(var i in k){
|
||||||
if(l == "pass"){ goto TURN_TURNS; }
|
if(i.Length < 2){
|
||||||
string[] k = l.Split("->");
|
x1 = 0;
|
||||||
foreach(var i in k){
|
x2 = 0;
|
||||||
if(i.Length < 2){ goto INPUT; }
|
y1 = 0;
|
||||||
|
y2 = 0;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
uint x1, y1, x2, y2;
|
}
|
||||||
x1 = (uint)(k[0][0] - 96);
|
x1 = (uint)(k[0][0] - 96);
|
||||||
x2 = (uint)(k[1][0] - 96);
|
x2 = (uint)(k[1][0] - 96);
|
||||||
y1 = (uint)(k[0][1] - 48);
|
y1 = (uint)(k[0][1] - 48);
|
||||||
y2 = (uint)(k[1][1] - 48);
|
y2 = (uint)(k[1][1] - 48);
|
||||||
//Console.WriteLine("{0}, {1}; {2}, {3}", x1, y1, x2, y2);
|
Console.WriteLine("{0}, {1}; {2}, {3}", x1, y1, x2, y2);
|
||||||
|
return true;
|
||||||
if(!this._mozgat(x1, y1, x2, y2)){
|
}
|
||||||
return;
|
public bool mozgat(string l){
|
||||||
}
|
uint x1, y1, x2, y2;
|
||||||
|
|
||||||
TURN_TURNS:
|
if(_mozgat_parse(l, out x1, out y1, out x2, out y2) &&
|
||||||
if(this.mozgatgat == Szin.FEHER){
|
this._mozgat(x1, y1, x2, y2)){
|
||||||
this.mozgatgat = Szin.FEKETE;
|
this.turn_turns();
|
||||||
}else{
|
return true;
|
||||||
this.mozgatgat = Szin.FEHER;
|
}
|
||||||
}
|
return false;
|
||||||
}
|
}
|
||||||
// ### Állapot ellenőrzők ###
|
// ### Állapot ellenőrzők ###
|
||||||
public void print(){
|
public override string ToString(){
|
||||||
Console.WriteLine("\n\n");
|
string r = "";
|
||||||
// --- Tábla és bábúk ---
|
// --- Tábla és bábúk ---
|
||||||
for(uint i = Asztal.TERMET; i >= 1; i--){
|
for(uint i = Asztal.TERMET; i >= 1; i--){
|
||||||
|
|
||||||
Console.Write(" ");
|
r += " ";
|
||||||
for(uint h = 1; h <= Asztal.TERMET; h++){
|
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++){
|
for(uint h = 1; h <= Asztal.TERMET; h++){
|
||||||
Console.Write(Asztal.HKOCKA_ELEM);
|
r += Asztal.HKOCKA_ELEM;
|
||||||
if(this[h, i] != null){
|
if(this[h, i] != null){
|
||||||
Console.Write(this[h, i].Jel);
|
r += this[h, i].Jel;
|
||||||
}else{
|
}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++){
|
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(" ");
|
r += " ";
|
||||||
for(uint i = 0; i < Asztal.TERMET; i++){
|
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 ---
|
// --- 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){
|
foreach(var i in this.halottak){
|
||||||
Console.Write(" " + i.Jel + ',');
|
r += " " + i.Jel + ',';
|
||||||
}
|
}
|
||||||
Console.WriteLine();
|
r += "\n";
|
||||||
|
|
||||||
// --- Lépés állapot ---
|
// --- Lépés állapot ---
|
||||||
Console.Write("\x1b[1mSoron következő játékos: \x1b[0m");
|
r += "\x1b[1mSoron következő játékos: \x1b[0m";
|
||||||
if(this.mozgatgat == Szin.FEHER){
|
if(this.mozgathat == Szin.FEHER){
|
||||||
Console.Write("□ ");
|
r += "□ ";
|
||||||
}else{
|
}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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,59 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Threading;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
class Program{
|
static class Program{
|
||||||
static Random r = new Random();
|
|
||||||
|
static Client client = new Client();
|
||||||
|
|
||||||
static void Main(string[] args){
|
static void Main(string[] args){
|
||||||
Asztal tabla = new Asztal(Jatekmod.klasszikus);
|
do{
|
||||||
|
if(client.doConnect()){
|
||||||
while(true){
|
client.Main();
|
||||||
tabla.print();
|
}
|
||||||
tabla.mozgat();
|
Thread.Sleep(1000);
|
||||||
}
|
}while(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,8 @@ public class AtloInfo{
|
|||||||
}
|
}
|
||||||
public void Calc(uint x, uint y){
|
public void Calc(uint x, uint y){
|
||||||
this.xy = (int)x - (int)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;
|
this.yx = (int)y + (int)x;
|
||||||
Console.WriteLine("{0} + {1} -> {2}", y, x, this.yx);
|
//Console.WriteLine("{0} + {1} -> {2}", y, x, this.yx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
129
src/Server.cs
129
src/Server.cs
@ -1,7 +1,134 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
class Server{
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
class Program{
|
static class ServerProgram{
|
||||||
static Random r = new Random();
|
|
||||||
|
static Server server = new Server();
|
||||||
|
|
||||||
static void Main(string[] args){
|
static void Main(string[] args){
|
||||||
Asztal tabla = new Asztal(Jatekmod.klasszikus);
|
Byte[] buffer = new Byte[256];
|
||||||
|
String data = null;
|
||||||
|
|
||||||
while(true){
|
server.Start();
|
||||||
tabla.print();
|
server.MakeMatch();
|
||||||
tabla.mozgat();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,8 @@ using System;
|
|||||||
|
|
||||||
while(true){
|
while(true){
|
||||||
tabla.print();
|
tabla.print();
|
||||||
tabla.mozgat();
|
Console.Write(": ");
|
||||||
|
tabla.mozgat(Console.ReadLine());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user