Archived

This topic is now archived and is closed to further replies.

Question for newbies with some experience

This topic is 5175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two questions that go with each other: How far have you gotten, programming wise and what is your most proud accomplishment? I ask this because Ive been programming for months now, and I have not one single program more than 200 lines that is finished. I started a couple, but none of them are finished (mainly because the design of them sucked and they were going no where anyway). Right now I am working on a polished version of TicTacToe. It may not sound like much, but this will be my first completely OOP program, and it is already going pretty well. I plan to have 3 different modes (single, two, and demo) and MAYBE more than 1 difficulty setting, but that isnt planned right now. What about you? Im still a newbie I guess (pointers are a bitch, and I cant do anything with classes short of the simple stuff), so the TicTacToe game will be my baby for a bit after I finish it and before I move only SDL .

Share this post


Link to post
Share on other sites
ive been doing it going on my 2nd year....during my first few months i was able to complete tic tac toe, blackjack, and really sry AI verion of Spades. about 6 months into it me and my friend started to make a texted baseed rpg. Was going really well had a nice battle system..the story could rival that of about FF8 and it was going good. spent a few weeks working on a well made inventory and shop system...but he moved and it was disbanded. now im trying to learn some graphics but going nowhere since i really cant grip the concept

Share this post


Link to post
Share on other sites
Ive just restarted programming and I have almost completed a movie logging system and will be moving on to a tic/tac/toe or 21 game when Iam finished. I dont plan to get much into graphic programming as my goal is it get into network programming for servers and clients of games and game applications. For now Iam just working on simple C++ console crap.

RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios

Share this post


Link to post
Share on other sites
I am most proud of my tic-tac-toe game I did in java. Although I wish I didn't make that class just to store globals, but other than that it's my best OO program to date:

// Tictactoe.java

// first java game (in DOS)


/* Description:
* A simple tic-tac-toe game with player vs. player,
* player vs. computer, or computer vs. computer
* capabilities. If one of the selections has one or
* more computer players you are asked for the
* difficulty level of each one.
*/


import java.io.*;
import java.util.*;

interface Globals
{
public static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
public static Random rand=new Random();
}

class Game
{
// player types

private static final byte[] PLAYERTYPE={0, // human

1, // human

2, // computer

3, // computer

};
private char[] board;

private byte turn=0;

private byte[] difficulty={0,0};

private byte[] players={PLAYERTYPE[0],PLAYERTYPE[1]};

private char[] chars={'X','O'};

private int index=0;

public Game(byte[] difficulty,byte mode,char[] chars)
{
board=new char[9];
for(int i=0;i<board.length;i++)
board[i]=' ';

this.difficulty=difficulty;
this.chars=chars;

// set mode

players[0]=PLAYERTYPE[mode];
players[1]=PLAYERTYPE[mode+1];

int i=Globals.rand.nextInt(2);
if(i==1)
{
byte temp=players[0];
char temp=chars[0];
players[0]=players[1];
players[1]=temp;
chars[0]=chars[1];
chars[1]=temp;
}
}

// displays the board

private void displayBoard()
{
// draw the board like this:

/*0|1|2
*-+-+-
*3|4|5
*-+-+-
*6|7|8
*/

String totalBoard="\n"+board[0]+"|"+board[1]+"|"+board[2]+
"\t\t1|2|3\n-+-+-\t\t-+-+-\n"+board[3]+"|"+board[4]+"|"+board[5]+
"\t\t4|5|6\n-+-+-\t\t-+-+-\n"+board[6]+"|"+board[7]+"|"+board[8]+
"\t\t7|8|9\n";
System.out.println(totalBoard);
}

// gets user input and adds the char to the board

private void getInput()
throws IOException
{
String out="Where do you want to go";
if(players[0]<2&&players[1]<2)
out+=" Player "+(turn+1)+"? ";
else
out+="? ";
System.out.print(out);
String input;
while((input=Global.in.readLine())==null);
int move=Integer.parseInt(input);
boolean valid=false;
while(!valid)
{
if(move<10&&move>0)
{
if(board[(move-1)]==' ')
{
valid=true;
board[(move-1)]=chars[turn];
}
}
if(!valid)
{
System.out.println("That is not a valid choice.");
System.out.print("Please choose again: ");
while((input=Global.in.readLine())==null);
move=Integer.parseInt(input);
}
}
}

// do the AI

private void doAI()
{
if(difficulty[turn]==0)
{
// easy, so just randomly plot the letter

// unless the player is going to win, then

// block



// check to see if the computer can win


int num=0;
int space=0;

// check horizontally

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[i*3+j]==chars[turn])
num++;
else
space=j;
}
if(num==2&&board[i*3+space]==' ')
{
// go for the win

board[i*3+space]=chars[turn];
return;
}
}

// check vertically

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[j*3+i]==chars[turn])
num++;
else
space=j;
}
if(num==2&&board[space*3+i]==' ')
{
// go for the win

board[space*3+i]=chars[turn];
return;
}
}

// check diagonally


num=0;
space=0;
for(int i=0;i<9;i+=4)
{
if(board[i]==chars[turn])
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// go for the win

board[space]=chars[turn];
return;
}

// check diagonally (the other way)

num=0;
space=0;
for(int i=2;i<7;i+=2)
{
if(board[i]==chars[turn])
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// go for the win

board[space]=chars[turn];
return;
}

// now check to see if the player is going to win


// check horizontally

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[i*3+j]!=chars[turn]&&board[i*3+j]!=' ')
num++;
else
space=j;
}
if(num==2&&board[i*3+space]==' ')
{
// block

board[i*3+space]=chars[turn];
return;
}
}

// check vertically

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[j*3+i]!=chars[turn]&&board[j*3+i]!=' ')
num++;
else
space=j;
}
if(num==2&&board[space*3+i]==' ')
{
// block

board[space*3+i]=chars[turn];
return;
}
}

// check diagonally


num=0;
space=0;
for(int i=0;i<9;i+=4)
{
if(board[i]!=chars[turn]&&board[i]!=' ')
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// block

board[space]=chars[turn];
return;
}

// check diagonally (the other way)

num=0;
space=0;
for(int i=2;i<7;i+=2)
{
if(board[i]!=chars[turn]&&board[i]!=' ')
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// block

board[space]=chars[turn];
return;
}

// nowhere to block or win, so just go at a random

// spot


while(true)
{
int spot=Globals.rand.nextInt(9);
if(board[spot]==' ')
{
board[spot]=chars[turn];
break;
}
}
}
else if(difficulty[turn]==1)
{
// medium AI


// check to see if the computer can win


int num=0;
int space=0;

// check horizontally

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[i*3+j]==chars[turn])
num++;
else
space=j;
}
if(num==2&&board[i*3+space]==' ')
{
// go for the win

board[i*3+space]=chars[turn];
return;
}
}

// check vertically

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[j*3+i]==chars[turn])
num++;
else
space=j;
}
if(num==2&&board[space*3+i]==' ')
{
// go for the win

board[space*3+i]=chars[turn];
return;
}
}

// check diagonally


num=0;
space=0;
for(int i=0;i<9;i+=4)
{
if(board[i]==chars[turn])
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// go for the win

board[space]=chars[turn];
return;
}

// check diagonally (the other way)

num=0;
space=0;
for(int i=2;i<7;i+=2)
{
if(board[i]==chars[turn])
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// go for the win

board[space]=chars[turn];
return;
}

// now check to see if the player is going to win


// check horizontally

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[i*3+j]!=chars[turn]&&board[i*3+j]!=' ')
num++;
else
space=j;
}
if(num==2&&board[i*3+space]==' ')
{
// block

board[i*3+space]=chars[turn];
return;
}
}

// check vertically

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[j*3+i]!=chars[turn]&&board[j*3+i]!=' ')
num++;
else
space=j;
}
if(num==2&&board[space*3+i]==' ')
{
// block

board[space*3+i]=chars[turn];
return;
}
}

// check diagonally


num=0;
space=0;
for(int i=0;i<9;i+=4)
{
if(board[i]!=chars[turn]&&board[i]!=' ')
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// block

board[space]=chars[turn];
return;
}

// check diagonally (the other way)

num=0;
space=0;
for(int i=2;i<7;i+=2)
{
if(board[i]!=chars[turn]&&board[i]!=' ')
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// block

board[space]=chars[turn];
return;
}

// no place to win or block so check for

// trick conditions


int other=(turn==1)?0:1;

if(board[0]==chars[other]||board[2]==chars[other])
{
if(board[1]==' ')
{
board[1]=chars[turn];
return;
}
}
else if(board[6]==chars[other]||board[8]==chars[other])
{
if(board[7]==' ')
{
board[7]=chars[turn];
return;
}
}

while(true)
{
int spot=Globals.rand.nextInt(9);
if(board[spot]==' ')
{
board[spot]=chars[turn];
break;
}
}
}
else
{
// check to see if the computer can win


int num=0;
int space=0;

// check horizontally

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[i*3+j]==chars[turn])
num++;
else
space=j;
}
if(num==2&&board[i*3+space]==' ')
{
// go for the win

board[i*3+space]=chars[turn];
return;
}
}

// check vertically

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[j*3+i]==chars[turn])
num++;
else
space=j;
}
if(num==2&&board[space*3+i]==' ')
{
// go for the win

board[space*3+i]=chars[turn];
return;
}
}

// check diagonally


num=0;
space=0;
for(int i=0;i<9;i+=4)
{
if(board[i]==chars[turn])
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// go for the win

board[space]=chars[turn];
return;
}

// check diagonally (the other way)

num=0;
space=0;
for(int i=2;i<7;i+=2)
{
if(board[i]==chars[turn])
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// go for the win

board[space]=chars[turn];
return;
}

// now check to see if the player is going to win


// check horizontally

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[i*3+j]!=chars[turn]&&board[i*3+j]!=' ')
num++;
else
space=j;
}
if(num==2&&board[i*3+space]==' ')
{
// block

board[i*3+space]=chars[turn];
return;
}
}

// check vertically

for(int i=0;i<3;i++)
{
num=0;
space=0;
for(int j=0;j<3;j++)
{
if(board[j*3+i]!=chars[turn]&&board[j*3+i]!=' ')
num++;
else
space=j;
}
if(num==2&&board[space*3+i]==' ')
{
// block

board[space*3+i]=chars[turn];
return;
}
}

// check diagonally


num=0;
space=0;
for(int i=0;i<9;i+=4)
{
if(board[i]!=chars[turn]&&board[i]!=' ')
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// block

board[space]=chars[turn];
return;
}

// check diagonally (the other way)

num=0;
space=0;
for(int i=2;i<7;i+=2)
{
if(board[i]!=chars[turn]&&board[i]!=' ')
num++;
else
space=i;
}
if(num==2&&board[space]==' ')
{
// block

board[space]=chars[turn];
return;
}

// now check for 'special cases'


int other=(turn==1)?0:1;

if(board[0]==chars[other]||board[2]==chars[other])
{
if(board[1]==' ')
{
board[1]=chars[turn];
return;
}
}
else if(board[6]==chars[other]||board[8]==chars[other])
{
if(board[7]==' ')
{
board[7]=chars[turn];
return;
}
}
num=0;
for(int i=0;i<9;i++)
if(board[i]!=' ')
num++;
if(num==0)
{
int[] moves={0,2,6,8};
index=Globals.rand.nextInt(4);
board[(moves[index])]=chars[turn];
return;
}
else if(num==(2+turn))
{
int[] opposites={8,6,2,0};
if(board[(opposites[index])]==' ')
{
board[(opposites[index])]=chars[turn];
return;
}
}
else if(num==(4+turn))
{
int[] adjacents={2,6,0,8,0,8,2,6};
int i=Globals.rand.nextInt(2);
if(board[(adjacents[(index*2)])]==' '&&
board[(adjacents[(index*2+1)])]==' ')
{
board[(adjacents[(index*2+i)])]=chars[turn];
return;
}
else if(board[(adjacents[(index*2)])]==' ')
{
board[(adjacents[(index*2)])]=chars[turn];
return;
}
else if(board[(adjacents[(index*2+1)])]==' ')
{
board[(adjacents[(index*2+1)])]=chars[turn];
return;
}
}
while(true)
{
int spot=Globals.rand.nextInt(9);
if(board[spot]==' ')
{
board[spot]=chars[turn];
break;
}
}
}
}

// main function, plays the game

public void play()
throws IOException,InterruptedException
{
while(true)
{
// display the board

displayBoard();

// get input if current player is a human

if(players[turn]<2)
{
getInput();
}
else
{
// do AI

doAI();
}

// now check to see if the current player has won

boolean won=false;

// check horizontally first

for(int i=0;i<3;i++)
{
if(board[i*3]==chars[turn]&&board[i*3+1]==chars[turn]&&
board[i*3+2]==chars[turn])
{
won=true;
break;
}
}

// next check vertically

for(int i=0;i<3;i++)
{
if(board[i]==chars[turn]&&board[3+i]==chars[turn]&&
board[6+i]==chars[turn])
{
won=true;
break;
}
}

// next check diagonally

if(board[0]==chars[turn]&&board[4]==chars[turn]&&
board[8]==chars[turn])
won=true;
else if(board[2]==chars[turn]&&board[4]==chars[turn]&&
board[6]==chars[turn])
won=true;
if(won)
{
String whoWon;

if(players[0]<2&&players[1]<2)
{
// two players, so say the player #

whoWon="Player "+(turn+1)+" won!\n";
}
else if(players[0]>1&&players[1]>1)
{
// two computers, so say the computer #

whoWon="Computer "+(turn+1)+" won!\n";
}
else
{
// there is only one player so just say

// 'you won' or 'you lose'.

if(players[turn]<2)
whoWon="You won!\n";
else
whoWon="You lose.\n";
}
displayBoard();
System.out.println(whoWon);
return;
}
// check to see if it is a cats game

boolean notCats=false;
for(int i=0;i<9;i++)
if(board[i]==' ')
{
notCats=true;
break;
}
if(!notCats)
{
displayBoard();
System.out.println("Cats game!\n");
// cats game, quit the game

return;
}
turn++;
if(turn>1)
turn=0;
if(players[0]>1&&players[1]>1)
{
// pause between each turn

// because its computer vs.

// computer

Thread.sleep(2000);
}
}
}
}

public class Tictactoe
{

public static void main(String args[])
{
try
{
OUTER:
while(true)
{
Game game;
byte[] difficulty={0,0};

// get mode

System.out.print("0) exit\n1) player vs. player\n2) player vs. computer\n"+
"3) computer vs. computer\n> ");
String input;
while((input=Globals.in.readLine())==null);
byte num=(byte)Integer.parseInt(input);
if(num==0)
{
return;
}
else if(num>3||num<0)
{
System.out.println("Invalid selection!");
continue;
}
else if(num==2)
{
// get the difficulty level for the one computer

System.out.print("Please select a difficulty level:\n1) easy\n"+
"2) medium\n3) hard\n");
while((input=Globals.in.readLine())==null);
difficulty[1]=(byte)Integer.parseInt(input);
difficulty[1]--;
if(difficulty[1]<0||difficulty[1]>2)
{
System.out.println("Invalid difficulty level!");
continue;
}
}
else if(num==3)
{
// get the difficulty level for both computers

for(int i=1;i<3;i++)
{
System.out.print("Please select a difficulty level for computer "+
i+": \n1) easy\n"+"2) medium\n3) hard\n");
while((input=Globals.in.readLine())==null);
difficulty[i-1]=(byte)Integer.parseInt(input);
difficulty[i-1]--;
if(difficulty[i-1]<0||difficulty[i-1]>2)
{
System.out.println("Invalid difficulty level!");
continue OUTER;
}
}
}

String output;

if(num==1)
output="What letter do you want to be Player 1? (X or O): ";
else if(num==2)
output="What letter do you want to be? (X or O): ";
else
output="What letter do you want Computer 1 to be? (X or O): ";

// get the first player's letter

System.out.print(output);

while((input=Globals.in.readLine())==null);

char charInput=input.charAt(0);

if(Character.isLowerCase(charInput))
charInput=Character.toUpperCase(charInput);

if(charInput!='X'&&charInput!='O')
{
System.out.println("The letter "+charInput+" is not a valid choice!");
continue;
}

char[] chars={charInput,(charInput=='X')?'O':'X'};
game=new Game(difficulty,(byte)(num-1),chars);
game.play();
}
}
catch(IOException e)
{
System.out.println("Error! Input exception occurred. quiting...");
return;
}
catch(Exception e)
{
System.out.println("Error! Exception occurred. quiting...");
return;
}
}
}

Also, it was my first ever program with exception handling (not like I had a choice, though :D). My paint program has a better design than this, but I haven't started programming it yet (I'm still in the technical doc stage).

EDIT: forgot to say that I made this a few months ago (about 9 months after I started programming with c++)

EDIT: oh, and if you get a chance to compile it, try running it on computer vs computer mode-its fun to watch


[edited by - brassfish89 on October 11, 2003 11:50:38 AM]

[edited by - brassfish89 on October 11, 2003 11:53:07 AM]

[edited by - brassfish89 on October 11, 2003 11:54:36 AM]

Share this post


Link to post
Share on other sites
About 1 year ago I started programming. My greatest accomplishment was a simple text-based game. Right now, I am working on an AI system which will be my greatest accomplishment.

Scott Simontis
e-mail:ageofscott@comcast.net
AIM:ssimontis

Share this post


Link to post
Share on other sites