Tic Tac Toe Multiplayer

Tic Tac Toe and Network Programming in C#

Page 1 of 1

13 Replies - 9827 Views - Last Post: 02 September 2010 - 07:50 AM Rate Topic: -----

#1 Guest_Jason*


Reputation:

Tic Tac Toe Multiplayer

Posted 28 August 2010 - 04:37 AM

Hello everybody,

I have a built a simple naughts and crosses game in which the human player plays against a computer AI. I now want to build a game with human and human functionality, which will presumably be even easier than building it with computer AI. Anyway, I want to be able to play this game over my home network, whilst the two players are on different computers. So, for example, my brother playing the game on his laptop against me who is playing it on my laptop. I have a networking c# book to guide me through the syntax and processes, so by no means am I asking you o write this for me.

I would just like some advice really before I start. How would you go about this? I will need to build a simple server to which each game client game connect? The clients will then send messages to the server, which will subsequently relay the meassge to the other client and update their screen? In what way would you guys go about this? Just in very general terms so I don't start going down the wrong path initially as I am very new to the networking side of C#. What is the best way of implementing this functionality? Everytime a button is pressed on one screen (to select a position to play on the tic tac toe board), a message needs to be sent to the other screen to effectively click the button automatically on the other screen, in order to update the screen.

Some general advice on what would be the most efficient way of doing this would be very much appreciated, just to get me started. I will then do the work and research to try and implement it and perhaps return if I get really stuck.

Thanks a lot everyone. Sorry if this seems like a pointless question, but networking is a really difficult area for me at the moment.

Is This A Good Question/Topic? 0

Replies To: Tic Tac Toe Multiplayer

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5956
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 04:48 AM

This Client-Server Chat Tutorial may be a good jumping-off point for you.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Jason*


Reputation:

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 04:50 AM

I guess, more specifically what I am struggling with here is what I send in these message from each client to the server. I can work out how to send the messages, but it is what these messages should actually contain that I am struggling with especially (I think!).
Was This Post Helpful? 0

#4 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5956
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 05:03 AM

I would guess you'd send the location in the 3x3 grid and the player sending the request? Something like this?

struct TokenLocation
{
    unsigned int row;
    unsigned int col;
}

struct PlayerMove
{
    unsigned int player;
    TokenLocation loc;
}

Was This Post Helpful? 0
  • +
  • -

#5 Guest_Jason*


Reputation:

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 05:18 AM

Thank you very much. That's given me a bit to think about.
Was This Post Helpful? 0

#6 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5956
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 05:23 AM

Note that you can represent that over the wire in a number of different ways. For example, you could use a string like this "player#:row#:column#", or you could use XmlSerialization if you wanted to "get fancy".
Was This Post Helpful? 0
  • +
  • -

#7 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 06:55 AM

Luckily for me, networking is my favourite part about programming, I remember when I wrote my first UDP chat program that worked at like 11 years old, the joy was immense. Hopefully I will be able to give you some helpful advice!

What you first have to decide is 1) whether you will run server/client or 2) peer to peer, that being, without a server.

I would recommend a none-server route, as you're only playing 1v1 so you may as well build this in. Have two modes on your game, one of them will be to 'create a lobby' and the other will be to 'join a lobby', simply have it wait for TCP connections on a certain port (choose something random and big like 23527). Then have the other application connect to this port and your IP. By doing this, you already choose who is the server and who is the client, or master and slave etc. This way you can decide who goes first as well.

Once you've connected, you will both be able to load the game screen, and initialise your variables, you should have a turn variable which is true for the master and false for the slave, once a move has been sent, you'd switch this variable around, this enables you to send your moves upon clicking etc.

As for the actual data you send, it really depends. Plain text will do, and all you have to do is think of a protocol, it's easy really. Get a piece of paper and write down what things the clients will need to communicate:

1) A move is being sent
So for this you would have to think what information also needs to be sent? The position that has been clicked, so X,Y could work. So you want prefix + delimiter + x + delimiter + y as a string. The delimiter can just be a character like | that you use to split strings with.

So you could send this:
MOVE|0,2
..then the client would parse move, then parse 0 to x and 2 to y, and know you've moved there.

2) A rematch is requested
For this, simply send:
REMATCH
and have the other client respond with something like
REMATCHOK
and then you'd fire the initialising events again and start again.

There are tonnes of things you could add to this, a chat maybe?

CHAT|{name}|{msg}

Where {name} would be your name and {msg} would be your message. I would recommend using Base64 encryption/decryption for sending strings like that, so that someone doesn't accidentally type a | into the chat and break the program!

Good luck!
Was This Post Helpful? 2
  • +
  • -

#8 Guest_Jason*


Reputation:

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 07:43 AM

Thank you hugely to both of you for replying.

Imdsm...that was awesome! I can totally see why networking is your favorite part of programming; even I can see that you can do so many really cool things with it. I just find, at the moment anyway, everything is just jumbled in my head with regards to networking, and I consequently find it very difficult to break networking problems up into logical steps. Hence why I am doing this project, i.e. to improve my networking skills and because, as you touched on actually, there are a number of really interesting things a can do with this (like adding chat...that would be awesome!). Anyway, I really appreciate the detail you went into there, it was a massive help, without obviously giving me all the answers on a plate!

The idea about peer to peer is definately the option I will (try to) implement; I hadn't actually thought of that option. Very helpful!

Thanks hugely again, those logical steps outlined in your post were just what I needed to get me started and begin,at the very least, to unjumble all the networking stuff in my head!

I'll get started now an see how I go!
Was This Post Helpful? 1

#9 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 10:57 AM

Glad to have been of assistance, if I can recommend something to do before you jump right into adding multi-player capabilities to your game, why not try a few simple apps first.

For example, try an app which has two buttons, listen and connect, learn to use TcpClient and TcpListener, or maybe even Socket (my favourite), and how the connect/disconnect events work.

Then try upgrading them to sending some text, and the other receiving it and displaying it in a message box.

Make one button listen, and the other connect, then find the exe (in bin\debug) and run it twice.

Maybe have it do a sort of ping pong, like so:

Client 2 listens on port 12345
Client 1 connects to 127.0.0.1:12345
1 -> HELLO
2 <- HELLOBACK
1 -> HOWAREYOU
2 <- GOODTHANKS
1 -> GOODBYE
2 <- CYALATER
Client 1 disconnects
Client 2 closes socket and recycles it to listen again

This would really help unjumble things I think, but bare with it, a few years ago when I tried multi-threading with delegates for the first time, I used to wake up at 4am mumbling code delirious, my mum actually thought I'd gone crazy at one point.. but less on that. Point is, everything will be confusing at first, but with time it'll sink in.

Good luck!
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Jason*


Reputation:

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 12:25 PM

Thanks again for your time and advice. I had actually, just prior to reading your post, created a WPF application with the listen and connect buttons you described! It isn't working as yet as I want as yet, but i'm reading through the appropriate sections in my networking books and really trying to piece everyhing together into an application that vaguely represents your example.

I also figured this 'start very simple' approach was the way to go too (in fact, the only possible way I think for me at the moment). I can see it very very gradually sinking in though,bit by bit, especially if I do what you suggested and keep working on it, I hope anyway. It is a difficult topic to get your head around to start with; it really is. I can see the general ideas behind networking and different protocols etc, but when it comes to the details (particuarly programming the details), I find myself getting really confused and muddled.

I remember having a similar, but not quite as bad, with delegates and events. They used to be a bit jumbled in my head.

I'm not giving up anytime soon though; that's not my style! I really had no sense of direction at all before your posts. I had no idea where to start, or even head. You've given me the bit of direction I needed to get me started.

Thanks again, really appreciate the help. It is just what I need to help me get my head around this topic once and for all!
Was This Post Helpful? 1

#11 Guest_Jason*


Reputation:

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 12:30 PM

With regrds to the Socket class, am I correct in saying that the TcpClient and TcpListener classes take care of socket creation behind the scenes, but using the Socket class explicitly gives you other functional advantages? I suppose it would be benefitial to me, as a learner, to use the Socket class so I get a feel to the details of creating sockets and such.
Was This Post Helpful? 0

#12 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 12:48 PM

The Socket class simply gives you a broader range of functionality, whereas the TcpClient and TcpListeners are designed for more specific functions.

Why not try both?
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Jason*


Reputation:

Re: Tic Tac Toe Multiplayer

Posted 28 August 2010 - 12:57 PM

Yes...that's probably the best thing to do thinking about it. I'll try and get the simple application with the listen and connect buttons working with both before I start the tic tac toe application. I'll see how I go!

Thanks :)
Was This Post Helpful? 0

#14 Guest_Jason*


Reputation:

Re: Tic Tac Toe Multiplayer

Posted 02 September 2010 - 07:50 AM

Right, I think I am almost in a position to start looking at building the game; just need to finalise details of Socket class first. Anyway, I am quite clear on how I am going to go about implementing the core networing capabilities of the game thanks to you guys. However I have one question.

Say a player creates and new lobby and is waiting for a connection from another player (making them the server effectively). The other player, on another computer in the LAN is preparing to click join lobby to connect to the listening server. At the moment, I am going to have to manually enter the IP address/host name of the computer acting as the server in order to connect. This isn't very flexible if I want to install the program on various computers as I have to hardcode the IP addresses and such into each application so that when acting as the client, it knows how to connect to the computer acting as the server. Therefore, is there any way I can connect to the server without having to manually enter the connection information of the server into the client?

Sorry if that's a bit confusing. Ideally, I would like a way to dynamically get the connection information of the computer acting as the server when the 'client' clicks join lobby to connect to the server. I'm not entirely sure how possible this is though?
Was This Post Helpful? 0

Page 1 of 1