Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Programming Challenge: Noughts and Crosses

Name: Anonymous 2009-08-11 16:23

I just got finished with my first program (that is, the first one I haven't just copied out of a book). It's a text based version of Noughts and Crosses (aka Tic-tac-toe) without an AI, and it looks pretty ugly to me. Commenting is half-assed, some variables are given nondescript names, and I can tell that my loop structure can use a little work. Still, it runs.

Show me up, /prog/. Write a more elegant version in the language of your choice. Surely it's not a difficult task for [o][u]EXPERT PROGRAMMERS[/o][/u] such as yourselves. If you've got any suggestions for me, those would be nice, too.

Name: Anonymous 2009-08-11 16:24


//Noughts and crosses

#include <iostream>
using namespace std;

//Displays board on screen
void dispboard (char d[9]) {
     cout << " " << d[0] << " | " << d[1] << " | " << d[2] << "\n";
     cout << "---+---+---\n";
     cout << " " << d[3] << " | " << d[4] << " | " << d[5] << "\n";
     cout << "---+---+---\n";
     cout << " " << d[6] << " | " << d[7] << " | " << d[8] << "\n";
}

//Horizontal checker function of checkboard
int horicheck (char x[9], char y, int z) {
     if (x[z] == y) {
             if (x[z+1] == y) {
                       if (x[z+2] == y)
                          return 1;
                      
                       else
                           return 0;
             }
             else
                 return 0;
     }
     else
         return 0;
}

//Vertical checker function of checkboard
int verticheck (char x[9], char y, int z) {
     if (x[z] == y) {
             if (x[z+3] == y) {
                       if (x[z+6] == y)
                          return 1;
                      
                       else
                           return 0;
             }
             else
                 return 0;
     }
     else
         return 0;
}

//Top-left to bottom-right diagonal checker function of checkboard
int diagcheck1 (char x[9], char y, int z) {
     if (x[z] == y) {
             if (x[z+4] == y) {
                       if (x[z+8] == y)
                          return 1;
                      
                       else
                           return 0;
             }
             else
                 return 0;
     }
     else
         return 0;
}

//Top-right to bottom-left diagonal checker function of checkboard
int diagcheck2 (char x[9],char y,int z) {
     if (x[z] == y) {
             if (x[z+2] == y) {
                       if (x[z+4] == y)
                          return 1;
                      
                       else
                           return 0;
             }
             else
                 return 0;
     }
     else
         return 0;
}
            
//Checks board for a win condition
//Uses horicheck, verticheck, diagcheck1, and diagcheck2
int checkboard (char a[], char b) {
     if (horicheck (a,b,0) == 1)
        return 1;
     else if (horicheck (a,b,3) == 1)
          return 1;
     else if (horicheck (a,b,6) == 1)
          return 1;
     else if (verticheck (a,b,0) == 1)
          return 1;
     else if (verticheck (a,b,3) == 1)
          return 1;
     else if (verticheck (a,b,6) == 1)
          return 1;
     else if (diagcheck1 (a,b,0) == 1)
          return 1;
     else if (diagcheck2 (a,b,2) == 1)
          return 1;
     else
         return 0;
}

int main () {
    char board[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    char curplayer = 'X';
    int done;
    char winner = 'q';
    int turn;
    int fin = 0;
   
    cout << "Noughts and Crosses\n\n";
   
    cout << " 0 | 1 | 2\n---+---+---\n 3 | 4 | 5\n---+---+---\n 6 | 7 | 8\n";
    cout << "To place your nought or cross in a square, type the location of\nits square, as seen above.\n\n";
   
    do {
        cout << curplayer << "'s turn!\n";
        dispboard (board);
        done = 0;
        while (done == 0) {
              cout << "Player " << curplayer << ", please type the location of the square that\n";
              cout << "you wish to place a '" << curplayer << "'this turn. ";
              cin >> turn;
              if (turn >= 0 && turn < 9) {
                 if (board[turn] == ' ') {
                    board[turn] = curplayer;
                    done = 1;
                    cout << "\n'" << curplayer << "' placed!\n";
                 }
                 else
                     cout << "\nThere is already a nought or cross at that location.\n";
              }
               
              else
                  cout << "\nYou did not specifiy a valid square. Locations range from 0 to 8,\nwith 0 being the upper left square and 8 being the bottom left.\n";
          }
         
          if (checkboard (board, 'X') == 1)
                         winner = 'X';
          else if (checkboard (board, 'Y') == 1)
               winner = 'O';
              
          else {
               if (curplayer == 'X')
                  curplayer = 'O';
               else
                   curplayer = 'X';
          }
    } while (winner == 'q');
   
    cout << "Player " << winner << " won the game!";
           
    return 0;
}

Name: Anonymous 2009-08-11 16:29

Cross my anus.

Name: Anonymous 2009-08-11 16:32

I had an ncurses version somewhere. Upon misclicking on the grid rather than the space in-between, the program would print ``are you fucking blind'' and proceed to delete files in user's home. I'll try to find it.

Name: Anonymous 2009-08-11 16:57

>>2
using namespace std;
Stopped reading right there.

Name: Anonymous 2009-08-11 17:12

>>5

Give me a break, I just started reading my book yesterday.

Name: Anonymous 2009-08-11 17:28

>>6
There was a time, there was a place, where smacking the student on the hand was considered proper. Nowadays you can't even use implications.

Tell you what, why don't I just code for you, dear, you run along and play.

Name: Anonymous 2009-08-11 17:41

>>7

That's exactly the point of this thread, I want to see you code.

Name: Anonymous 2009-08-11 18:17

>>6
Use a condom. And K&R.

Name: Anonymous 2009-08-11 18:17

/print

[ ][ ][ ]
[ ][ ][ ]
[ ][ ][ ]

Name: Smug Haskell Weenie 2009-08-11 18:37

import Control.Applicative
import Control.Monad
import Control.Monad.Instances
import Data.List
import Data.Monoid

data Player = None | X | O deriving (Show, Eq)
other X = O
other _ = X
type Board = [[Player]]
instance Monoid Player where
    mempty = None
    None `mappend` a = a
    a `mappend` b = a

initialBoard = replicate 3 (replicate 3 None)
showTile n None = show n
showTile _ p = show p
showRow n = intercalate " | " . zipWith showTile [n..]

drawBoard :: Board -> String
drawBoard = unlines . intersperse "----------" . zipWith showRow [1,4..]

winner = (mconcat .) . sequence $
    [id, (. transpose)] <*> ([id, (. diagonal)] <*> (checkRows <$> [X, O]))

checkRows player = maybe None (const player) . find (all (== player))
diagonal = return . concatMap (take 1) . zipWith id (iterate (tail .) id)

updateNth n f = zipWith id (replicate n id ++ [f] ++ repeat id)
update n player = updateNth (n `div` 3) $ updateNth (n `mod` 3) (const player)

getMove player board = do
    n <- readLn
    if n `elem` [1..9]
        then if concat board !! (n - 1) == None
            then return $ update (n - 1) player board
            else putStrLn "Already taken." >> getMove player board
        else putStrLn "1 to 9, please." >> getMove player board

main = do
    putStrLn $ drawBoard initialBoard
    loop X initialBoard
    where
        loop player board = do
            putStrLn $ show player ++ " to play, choose a numbered square: "
            board' <- getMove player board
            putStrLn $ drawBoard board'
            case winner board' of
                None -> if all (all (/= None)) board'
                    then putStrLn "It's a draw!"
                    else loop (other player) board'
                w -> putStrLn $ "The winner is: " ++ show w

Name: Anonymous 2009-08-11 18:58

#include <stdlib.h>
#include <curses.h>
#include <signal.h>

char board[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int x_win = 0, o_win = 0;

void done(int _)
{
    exit(endwin());
}

int play_turn(char c)
{
    mvaddch(3, 14, c);

    switch (getch()) {
        case KEY_A1:
            if (board[0][0] == 0) {
                mvaddch(1, 2, c);
                return 0;
            } else return -1;
        case KEY_UP:
            if (board[0][1] == 0) {
                mvaddch(1, 6, c);
                return 0;
            } else return -1;
        case KEY_A3:
            if (board[0][2] == 0) {
                mvaddch(1, 10, c);
                return 0;
            } else return -1;
        case KEY_LEFT:
            if (board[1][0] == 0) {
                mvaddch(3, 2, c);
                return 0;
            } else return -1;
        case KEY_B2:
            if (board[1][1] == 0) {
                mvaddch(3, 6, c);
                return 0;
            } else return -1;
        case KEY_RIGHT:
            if (board[1][2] == 0) {
                mvaddch(3, 10, c);
                return 0;
            } else return -1;
        case KEY_C1:
            if (board[2][0] == 0) {
                mvaddch(5, 2, c);
                return 0;
            } else return -1;
        case KEY_DOWN:
            if (board[2][1] == 0) {
                mvaddch(5, 6, c);
                return 0;
            } else return -1;
        case KEY_C3:
            if (board[2][2] == 0) {
                mvaddch(5, 10, c);
                return 0;
            } else return -1;
        default:
            return -1;
    }

    return 0;
}

int check_victory()
{
    int i, j, x, o;
    for (i = 0; i < 3; ++i) {
        int x_h, x_v, o_h, o_v;
        x_h = x_v = o_h = o_v = 0;

        for (j = 0; j < 3; ++j) {
            if (board[i][j] == 'X') ++x_h;
            if (board[j][i] == 'X') ++x_v;
            if (board[i][j] == 'O') ++o_h;
            if (board[j][i] == 'O') ++o_h;
        }

        if (x_h == 3 || x_v == 3) return x_win = 1;
        if (o_h == 3 || o_v == 3) return o_win = 1;
    }

    x = o = 0;
    if (board[0][0] == 'X') ++x;
    if (board[0][0] == 'O') ++o;
    if (board[1][1] == 'X') ++x;
    if (board[1][1] == 'O') ++o;
    if (board[2][2] == 'X') ++x;
    if (board[2][2] == 'O') ++o;
    if (x == 3) return x_win = 1;
    if (o == 3) return o_win = 1;

    x = o = 0;
    if (board[0][2] == 'X') ++x;
    if (board[0][2] == 'O') ++o;
    if (board[1][1] == 'X') ++x;
    if (board[1][1] == 'O') ++o;
    if (board[2][0] == 'X') ++x;
    if (board[2][0] == 'O') ++o;
    if (x == 3) return x_win = 1;
    if (o == 3) return o_win = 1;

    return 0;
}

int main(int argc, char **argv)
{
    int i;

    signal(SIGINT, done);

    initscr();
    keypad(stdscr, TRUE);
    nonl();
    cbreak();
    noecho();

    mvaddch(1, 4, '|');
    mvaddch(1, 8, '|');
    mvaddch(3, 4, '|');
    mvaddch(3, 8, '|');
    mvaddch(5, 4, '|');
    mvaddch(5, 8, '|');

    for (i = 1; i < 12; ++i) {
        mvaddch(2, i, '-');
        mvaddch(4, i, '-');
    }

    mvaddch(2, 4, '+');
    mvaddch(2, 8, '+');
    mvaddch(4, 4, '+');
    mvaddch(4, 8, '+');

    for (;;) {
        play_turn('X');
        if (check_victory()) break;
        play_turn('Y');
        if (check_victory()) break;
    }

    done(0);
    return 0;
}

Name: Anonymous 2009-08-11 19:01

printf("A strange game. The only winning move is not to play");

Name: CURSES MEME FAN 2009-08-11 19:04

>>12
Excellent.

Name: Anonymous 2009-08-11 20:41

>>13
hell no

Name: Anonymous 2009-08-11 23:12

if (board[i][j] == 'O') ++o_h;
if (board[j][i] == 'O') ++o_h;
FROZEN VOID QUALITY

Name: Anonymous 2009-08-11 23:34

>>16

FROZEN VOID QUALITY? WHY THAT IS ENTERPRISE PROGRAMMING.

Name: Anonymous 2009-08-11 23:44

>>17
I guess I could concede that enterprise programming is indeed an alias for buggy programming. None the less I think FROZEN VOID quality is more appropriate here.

Name: Anonymous 2009-08-12 1:21

#lang scheme

(require srfi/1 srfi/43)

(define board (make-parameter #f))
(define turn (make-parameter #f))

(define other -)
(define empty-board (make-vector 9 #f))

(define (minimax)
  (define (maximal)
    (cond ((value) => (lambda (v) (* (turn) v)))
          (else
           (do ((steps (step) (cdr steps))
                (maxi -inf.0
                      (max maxi
                           (-
                            (parameterize ((board (car steps))
                                           (turn (other (turn))))
                              (maximal))))))
             ((or (null? steps) (= 1 maxi)) maxi)))))
 
  (car (sort (step) < #:cache-keys? #t
             #:key (lambda (b)
                     (parameterize ((board b) (turn (other (turn))))
                       (maximal))))))

(define (value)
  (define lines
    '((0 1 2)
      (3 4 5)
      (6 7 8)
     
      (0 3 6)
      (1 4 7)
      (2 5 8)
     
      (0 4 8)
      (2 4 6)))
  (cond
    ((any
      (lambda (a) (and (equal? (car a) (cadr a)) (equal? (cadr a) (caddr a)) (car a)))
      (map (lambda (x) (map (lambda (y) (vector-ref (board) y)) x)) lines))
     => values)
    ((vector-every values (board)) 0)
    (else #f)))

(define (step)
  (filter
   values
   (sort
    (vector->list
     (vector-map
      (lambda (i x)
        (and (not x)
             (let ((temp (vector-copy (board))))
               (vector-set! temp i (turn))
               temp)))
      (board)))
    < #:key (lambda (x) (random)) #:cache-keys? #t)))

(define (format)
  (apply
   printf "\
~c|~c|~c
-----
~c|~c|~c
-----
~c|~c|~c

"
   (map
    (lambda (x) (case x ((1) #\X) ((-1) #\O) ((#f) #\ )))
    (vector->list (board)))))

(define (play myturn)
  (parameterize ((board (vector-copy empty-board)) (turn 1))
    (let loop ()
      (format)
      (cond
        ((value) => (lambda (w) (display (cond ((zero? w) "It's a tie!\n")
                                               ((= myturn w) "You won!\n")
                                               (else "You lost!\n")))))
        ((= (turn) myturn)
         (display "Enter square number (1-9): ")
         (let ((num (read)))
           (if (or (not (integer? num)) (> num 9) (< num 1)
                   (vector-ref (board) (- num 1)))
               (begin
                 (display "Invalid input.\n")
                 (unless (eof-object? num) (loop)))
               (begin
                 (vector-set! (board) (- num 1) (turn))
                 (parameterize ((turn (other (turn))))
                   (loop))))))
        (else
         (parameterize ((board (minimax)) (turn (other (turn))))
           (loop)))))))

(display "Do you want to go first? (#f/#t) ")
(let ((first (read)))
  (play (if first 1 -1)))

Name: Anonymous 2009-08-12 1:24

>>18
I find it inappropriate, as I as a principle object to giving trolls the attention they crave.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-08-12 5:24

OP try: http://www.programmersheaven.com/download/1020/0/ZipView.aspx



______________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Velox Et Astrum gamedev forum: http://etastrum.phpbb3now.com
Wit is sharper than the sharpest dagger.

Name: Anonymous 2009-08-12 6:01

import java.util.Scanner;
import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Random;
public class NC {
    private static final BigInteger WIDTH = BigInteger.valueOf(10);
    private static final BigInteger HEIGHT = BigInteger.valueOf(10);
    private static final BigInteger WINS = BigInteger.valueOf(3);
    private static final BigInteger PLAYERS = BigInteger.valueOf(2);
    public static Scanner sc;
    public static void main(String[] args) {
        BigInteger i;
        sc = new Scanner(new BufferedInputStream(System.in));
        ArrayList<Player> players = new ArrayList<Player>();
        for(i = BigInteger.ZERO; i.compareTo(PLAYERS) < 0; i = i.add(BigInteger.ONE)) {
            System.out.print("Please enter the name of player "+EnglishCounter.bigIntToEnglish(i.add(BigInteger.ONE)).trim()+": ");
            players.add(new Player(sc.nextLine(), i));
        }
        Board b = new Board(players, WIDTH, HEIGHT, WINS);
    }
}
class Board {
    private BigInteger width, height, wins, moveCount;
    private ArrayList<Player> players;
    private ArrayList<ArrayList<Square>> squares;
    private Player firstToMove;
    public Board(ArrayList<Player> players, BigInteger width, BigInteger height, BigInteger wins) {
        this.width = width;
        this.height = height;
        this.wins = wins;
        this.players = players;
        this.firstToMove = players.get(new Random().nextInt(players.size()));
        this.moveCount = BigInteger.ZERO;
        this.squares = new ArrayList<ArrayList<Square>>();
        for(BigInteger i = BigInteger.ZERO; i.compareTo(width) < 0; i = i.add(BigInteger.ONE)) {
            ArrayList<Square> temp = new ArrayList<Square>();
            for(BigInteger j = BigInteger.ZERO; j.compareTo(height) < 0; j = j.add(BigInteger.ONE)) {
                temp.add(new Square());
            }
            this.squares.add(temp);
        }
        playGame();
    }
    private void playGame() {
        boolean moved;
        drawBoard();
        mainLoop:
        for(;;) {
            moved = false;
            for(BigInteger i = BigInteger.valueOf(players.indexOf(firstToMove)); !i.equals(BigInteger.valueOf(players.indexOf(firstToMove))) || !moved;
            i = i.add(BigInteger.ONE).mod(BigInteger.valueOf(players.size()))) {
                moved = true;
                doMove(players.get(i.intValue())); /* Narrowing conversion: TODO, reimplement java memory allocator to use a biginteger addressing scheme */
                drawBoard();
                if(checkWinner()) {
                    System.out.println("We have a winner!");
                    System.out.println("Congratulations: "+players.get(i.intValue()).getName());
                    break mainLoop;
                }
                else if(checkDraw()) {
                    System.out.println("It's a draw :(");
                    break mainLoop;
                }
            }
        }
    }
    private boolean checkWinner() {
        return checkheight() || checkwidth() || checkDiagDown() || checkDiagUp();
    }
    private boolean checkDraw() {
        return moveCount.compareTo(width.multiply(height)) >= 0;
    }
    private boolean matchPlayers(ArrayList<Player> players) {
        if(players.size() == 0) return false;
        Player lastPlayer = players.get(0);
        BigInteger count = BigInteger.ONE;
        for(BigInteger i = BigInteger.ONE; i.compareTo(BigInteger.valueOf(players.size())) < 0; i = i.add(BigInteger.ONE)) {
            Player current = players.get(i.intValue());
            if(lastPlayer != null && lastPlayer.equals(current)) {
                count = count.add(BigInteger.ONE);
                if(count.equals(wins)) return true;
            }
            else {
                lastPlayer = current;
                count = current == null ? BigInteger.ZERO : BigInteger.ONE;
            }
        }
        return false;
    }
    private boolean checkheight() {
        ArrayList<Player> players = new ArrayList<Player>();
        for(BigInteger i = BigInteger.ZERO; i.compareTo(height) < 0; i = i.add(BigInteger.ONE)) {
            for(BigInteger j = BigInteger.ZERO; j.compareTo(width) < 0; j = j.add(BigInteger.ONE)) {
                players.add(this.squares.get(i.intValue()).get(j.intValue()).getOwner());
            }
            if(matchPlayers(players)) return true;
            players = new ArrayList<Player>();
        }
        return false;
    }
    private boolean checkwidth() {
        ArrayList<Player> players = new ArrayList<Player>();
        for(BigInteger i = BigInteger.ZERO; i.compareTo(height) < 0; i = i.add(BigInteger.ONE)) {
            for(BigInteger j = BigInteger.ZERO; j.compareTo(width) < 0; j = j.add(BigInteger.ONE)) {
                players.add(this.squares.get(j.intValue()).get(i.intValue()).getOwner());
            }
            if(matchPlayers(players)) return true;
            players = new ArrayList<Player>();
        }
        return false;
    }
    private boolean checkDiagDown() {
        for(BigInteger k = wins.subtract(BigInteger.ONE); k.compareTo(height.add(width).subtract(wins)) < 0; k = k.add(BigInteger.ONE)) {
            BigInteger max = ((k.compareTo(width)>=0 && k.compareTo(height)<=0) ||
                (width.subtract(BigInteger.ONE).subtract(k).compareTo(BigInteger.ZERO)<0) ?
                    height.subtract(BigInteger.ONE).subtract(k).add(width).subtract(BigInteger.ONE) : height.subtract(BigInteger.ONE));
            BigInteger start = k.compareTo(height) >= 0 ? BigInteger.ZERO : height.subtract(BigInteger.ONE).subtract(k);
            ArrayList<Player> players = new ArrayList<Player>();
            for(BigInteger i = start; i.compareTo(max) <= 0; i = i.add(BigInteger.ONE)) {
                BigInteger j = i.subtract(height.subtract(BigInteger.ONE).subtract(k));
                players.add(this.squares.get(i.intValue()).get(j.intValue()).getOwner());
            }
            if(matchPlayers(players)) return true;
        }
        return false;
    }
    private boolean checkDiagUp() {
        for(BigInteger k = wins.subtract(BigInteger.ONE); k.compareTo(height.add(width).subtract(wins).subtract(BigInteger.valueOf(2)))<=0; k = k.add(BigInteger.ONE)) {
            BigInteger start = k.compareTo(height)>=0 ? height.subtract(BigInteger.ONE) : k;
            BigInteger min = ((k.compareTo(width)>=0 && k.compareTo(height)<=0) || k.subtract(width.subtract(BigInteger.ONE)).compareTo(BigInteger.ZERO)>0) ?
                k.subtract(width.subtract(BigInteger.ONE)) : BigInteger.ZERO;
            ArrayList<Player> players = new ArrayList<Player>();
            for(BigInteger i = start; i.compareTo(min)>=0; i = i.subtract(BigInteger.ONE)) {
                BigInteger j = k.subtract(i);
                players.add(this.squares.get(i.intValue()).get(j.intValue()).getOwner());
            }
            if(matchPlayers(players)) return true;
        }
        return false;
    }
    private void doMove(Player p) {
        System.out.println(p.getName()+", it is your turn! Please enter a number to move: ");
        BigInteger move;
        for(;;) {
            try {
                move = new BigInteger(NC.sc.nextLine());
            }
            catch(NumberFormatException e) {
                System.out.println("You must enter a valid number!");
                continue;
            }
            if(move.compareTo(BigInteger.ZERO) < 0 || move.compareTo(width.multiply(height)) >= 0) {
                System.out.println("You must enter a valid number!");
                continue;
            }
            Square sq = squares.get(move.remainder(width).intValue()).get(move.divide(width).intValue());
            if(sq.getOwner() != null) {
                System.out.println(sq.getOwner().getName()+" has already moved in that square!");
                continue;
            }
            else {
                sq.setOwner(p);
                moveCount = moveCount.add(BigInteger.ONE);
                break;
            }
        }
    }
    private void drawBoard() {
        System.out.println();
        BigInteger length = Player.getMaxSymbolLength().compareTo(BigInteger.valueOf(width.multiply(height).subtract(BigInteger.ONE).toString().length())) < 0 ?
            BigInteger.valueOf(width.multiply(height).subtract(BigInteger.ONE).toString().length()) : Player.getMaxSymbolLength();
        String seperator = "";
        for(BigInteger i = BigInteger.ZERO; i.compareTo(width) < 0; i = i.add(BigInteger.ONE)) {
            if(!i.equals(BigInteger.ZERO)) seperator += "+";
            for(BigInteger j = BigInteger.ZERO; j.compareTo(length.add(BigInteger.valueOf(2))) < 0; j = j.add(BigInteger.ONE)) {
                seperator += "-";
            }
        }
        for(BigInteger i = BigInteger.ZERO; i.compareTo(height) < 0; i = i.add(BigInteger.ONE)) {
            if(!i.equals(BigInteger.ZERO)) System.out.println(seperator);
            for(BigInteger j = BigInteger.ZERO; j.compareTo(width) < 0; j = j.add(BigInteger.ONE)) {
                if(!j.equals(BigInteger.ZERO)) System.out.print("|");
                Player owner = squares.get(j.intValue()).get(i.intValue()).getOwner();
                BigInteger difference = length.subtract(BigInteger.valueOf(owner == null ? width.multiply(i).add(j).toString().length() : owner.getSymbol().length()));
                String padding = "";
                for(BigInteger k = BigInteger.ZERO; k.compareTo(difference.divide(BigInteger.valueOf(2))) < 0; k = k.add(BigInteger.ONE)) {
                    padding += " ";
                }
                System.out.print(" "+padding+(difference.mod(BigInteger.valueOf(2)).equals(BigInteger.ONE)?" ":""));
                System.out.print(owner == null ? i.multiply(width).add(j).toString() : owner.getSymbol());
                System.out.print(padding+" ");
            }
            System.out.println();
        }
        System.out.println();
    }
}
class Square {
    private Player owner;
    public Square() {
        this.owner = null;
    }
    public Player getOwner() {
        return owner;
    }
    public void setOwner(Player o) {
        owner = o;
    }
}

Name: Anonymous 2009-08-12 6:01

>>22

class Player {
    private static BigInteger maxSymbolLength = BigInteger.ZERO;
    private static final String symbols = "XOABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private String name;
    private String symbol;
    private BigInteger index;
    public Player(String name, BigInteger position) {
        this.name = name;
        this.index = position;
        BigInteger sLen = BigInteger.valueOf(symbols.length());
        String sym = "";
        do {
            BigInteger temp = position.mod(sLen);
            sym += symbols.charAt(temp.intValue());
            position = position.subtract(temp).divide(sLen);
        }
        while(position.compareTo(BigInteger.ZERO) > 0);
        this.symbol = sym;
        System.out.println(this.symbol);
        if(BigInteger.valueOf(sym.length()).compareTo(maxSymbolLength) > 0) maxSymbolLength = BigInteger.valueOf(sym.length());
    }
    public static BigInteger getMaxSymbolLength() {
        return maxSymbolLength;
    }
    public String getName() {
        return this.name;
    }
    public String getSymbol() {
        return this.symbol;
    }
    public boolean equals(Object o) {
        if(o == null) return false;
        if(o == this) return true;
        if(o.getClass() != this.getClass()) return false;
        return this.index.equals(((Player)o).index);
    }
}
class EnglishCounter { /* Courtesy of Anonymous */
    private static BigInteger num = BigInteger.ONE;
    private static String[] mantissa = {"zero",     "one",     "two",     "three",     "four",     "five",   "six",
                                       "seven",    "eight",   "nine",    "ten",       "eleven",   "twelve", "thirteen",
                                      "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
    private static String[] exponent = {"", "thousand", "million", "billion", "trillion", "quadrillion",
                                     "quintillion", "sexillion", "septillion", "octillion", "nonillion", "decillion",
                                     "undecillion", "duodecillion", "tredecillion","quattuordecillion", "quindecillion", "sexdecillion",
                                     "septdecillion", "octodecillion", "novemdecillion", "vigintillion", "unvigintillion", "duovigintillion",
                                     "trevigintillion", "quattuorvigintillion", "quinvigintillion", "sexvigintillion", "septvigintillion", "octovigintillion",
                                     "novemvigintillion", "trigintillion", "untrigintillion", "duotrigintillion", "tretrigintillion", "quattuortrigintillion",
                                     "quintrigintillion", "sextrigintillion", "septtrigintillion", "octotrigintillion", "novemtrigintillion", "quadragintillion",
                                     "unquadragintillion", "duoquadragintillion", "trequadragintillion", "quattuorquadragintillion", "quinquadragintillion", "sexquadragintillion",
                                     "septquadragintillion", "octoquadragintillion", "novemquadragintillion", "quinquagintillion", "unquinquagintillion", "duoquinquagintillion",
                                     "trequinquagintillion", "quattuorquinquagintillion", "quinquinquagintillion", "sexquinquagintillion", "septquinquagintillion",
                                     "octoquinquagintillion",
                                     "novemquinquagintillion", "sexagintillion", "unsexagintillion", "duosexagintillion", "tresexagintillion", "quattuorsexagintillion",
                                     "quinsexagintillion", "sexsexagintillion", "septsexagintillion", "octosexagintillion", "novemsexagintillion", "septuagintillion",
                                     "unseptuagintillion", "duoseptuagintillion", "treseptuagintillion", "quattuorseptuagintillion", "quinseptuagintillion", "sexseptuagintillion",
                                     "septseptuagintillion", "octoseptuagintillion", "novemseptuagintillion", "octogintillion", "unoctogintillion", "duooctogintillion",
                                     "treoctogintillion", "quattuoroctogintillion", "quinoctogintillion", "sexoctogintillion", "septoctogintillion", "octooctogintillion",
                                     "novemoctogintillion", "nonagintillion", "unnonagintillion", "duononagintillion", "trenonagintillion", "quattuornonagintillion",
                                     "quinnonagintillion", "sexnonagintillion", "septnonagintillion", "octononagintillion", "novemnonagintillion", "centillion"};
    private static String[] tens = {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    public static String bigIntToEnglish(BigInteger b) {
        StringBuilder s = new StringBuilder();
        for(int i=0; i<exponent.length; i++) {
            BigInteger n = BigInteger.valueOf(1000).pow(i);
            if(n.compareTo(b)>0) {
                n = BigInteger.valueOf(1000).pow(i-1);
                BigInteger m = b.divide(n);
                BigInteger r = b.subtract(m.multiply(n));
                int a = m.intValue() / 100;
                int c = m.intValue() % 100;
                if(a>0) {
                    s.append(mantissa[a]+" hundred");
                    if(c>0) s.append(" and ");
                }
                if(c>0) {
                    if(c<20) s.append(mantissa[c]);
                    else for(int j=0; j<tens.length; j++) {
                        if(30 + 10*j > c) {
                            s.append(tens[j]);
                            if(c%10!=0) s.append(" "+mantissa[c%10]);
                            break;
                        }
                    }
                }
                s.append(" "+exponent[i-1]);
                if(!r.equals(BigInteger.ZERO)) s.append(" "+bigIntToEnglish(r));
                return s.toString();
            }
        }
        return "Overflow";
    }
}

Name: Anonymous 2009-08-12 12:41

>>16
Oops. I couldn't actually test it because I don't have a numpad.

Name: Anonymous 2010-11-27 20:56

Don't change these.
Name: Email:
Entire Thread Thread List