コード例 #1
0
    public static void Main(string[] args)
    {
        Console.WriteLine("Started");
        //Validate Args were provided
        if (args.Length != 1)
        {
            Console.WriteLine("missing args");
            System.Environment.Exit(1);
        }

        //open data file
        InFile puzzleData = new InFile(args[0]);

        Console.WriteLine("File read.");
        //exit program if data file unreadable
        if (puzzleData.OpenError())
        {
            Console.WriteLine("Failed to open " + args[0]);
            System.Environment.Exit(1);
        }

        //process data file
        string[] data = new string[9];
        for (int i = 0; i < data.Length; i++)
        {
            data[i] = puzzleData.ReadLine();
        }

        for (int i = 0; i < data.Length; i++)
        {
            Console.WriteLine(data[i]);
        }
    }
コード例 #2
0
    public static void Main(string[] args)
    {
        //                                        check that arguments have been supplied
        if (args.Length != 2)
        {
            Console.WriteLine("missing args");
            System.Environment.Exit(1);
        }
        //                                        attempt to open data file
        InFile data = new InFile(args[0]);

        if (data.OpenError())
        {
            Console.WriteLine("cannot open " + args[0]);
            System.Environment.Exit(1);
        }
        //                                        attempt to open results file
        OutFile results = new OutFile(args[1]);

        if (results.OpenError())
        {
            Console.WriteLine("cannot open " + args[1]);
            System.Environment.Exit(1);
        }
        //                                        various initializations
        int    total       = 0;
        IntSet mySet       = new IntSet();
        IntSet smallSet    = new IntSet(1, 2, 3, 4, 5);
        string smallSetStr = smallSet.ToString();
        //                                        read and process data file
        int item = data.ReadInt();

        while (!data.NoMoreData())
        {
            total = total + item;
            if (item > 0)
            {
                mySet.Incl(item);
            }
            item = data.ReadInt();
        }
        //                                        write various results to output file
        results.Write("total = ");
        results.WriteLine(total, 5);
        results.WriteLine("unique positive numbers " + mySet.ToString());
        results.WriteLine("union with " + smallSetStr
                          + " = " + mySet.Union(smallSet).ToString());
        results.WriteLine("intersection with " + smallSetStr
                          + " = " + mySet.Intersection(smallSet).ToString());

        /* or simply
         * results.WriteLine("union with " + smallSetStr + " = " + mySet.Union(smallSet));
         * results.WriteLine("intersection with " + smallSetStr + " = " + mySet.Intersection(smallSet));
         */

        results.Close();
    } // Main
コード例 #3
0
ファイル: Sudoku.cs プロジェクト: TomMakkink/Compilers
    public static void Main(string[] args)
    {
        Screen.ClrScr();

        InFile data = new InFile(args[0]);

        if (data.OpenError())
        {
            Console.WriteLine("cannot open " + args[0]);
            System.Environment.Exit(1);
        }

        List <List <int> > Board = createBoard(data);

        List <List <IntSet> > predictionBoard = createPredicativeBoard(Board);

        predictionBoard = FilterRows(predictionBoard);
        predictionBoard = FilterCols(predictionBoard);

        IO.Write('\n');
        IO.WriteLine("Please may you about to begin a new Game of Sudoku.");
        //writeBoard();
        IO.WriteLine("Please may you you either type in (S)tart or (Q)uite");
        char T = IO.ReadChar();

        if (T == 'S' || T == 's')
        {
            writeBoard(Board);
            string trash = IO.ReadLine();
            while (!isGameOver(Board))
            {
                // Get input from user
                string [] userInput = getInputFromUser();
                while (!validateInput(userInput))
                {
                    IO.WriteLine("Invalid Input.");
                    userInput = getInputFromUser();
                }
                // List of valid input
                List <int> userMove = convertToIntList(userInput);
                Board[userMove[0]][userMove[1]] = userMove[2];
                writeBoard(Board);
            }
        }
        if (T == 'q' || T == 'Q')
        {
            Screen.ClrScr(); System.Environment.Exit(1);
        }
    }
コード例 #4
0
        static void Main(string[] args)
        {
            Program p = new Program();

            int[,] Board = new int[9, 9];

            //Read in Data file
            InFile data = new InFile(args[0]);

            if (data.OpenError())
            {
                Console.WriteLine("cannot open " + args[0]);
                Environment.Exit(1);
            }


            Board = p.ProcessDataFile(data);
        }
コード例 #5
0
        static void Main(string[] args)

        {
            int[,] bob = new int[9, 9];


            if (args.Length != 1)
            {
                Console.WriteLine("missing args");
                System.Environment.Exit(1);
            }
            //                             attempt to open data file
            InFile data = new InFile(args[0]);

            if (data.OpenError())
            {
                Console.WriteLine("cannot open " + args[0]);
                System.Environment.Exit(1);
            }

            for (int row = 0; row < 9; row++)
            {
                for (int col = 0; col < 9; col++)
                {
                    if (row < 9 && col < 9)
                    {
                        bob[row, col] = data.ReadInt();
                    }
                }
            }

            void printBoard(int[,] board)
            {
                Console.Write(String.Format("{0,-2}{1,-2}{2,-2}{3,-2}{4,-2}{5,-2}{6,-2}{7,-2}{8,-2}{9,-2}", "", 0, 1, 2, 3, 4, 5, 6, 7, 8));
                for (int row = 0; row < 9; row++)
                {
                    Console.WriteLine();
                    for (int col = 0; col < 9; col++)
                    {
                        if (col == 0)
                        {
                            Console.Write(String.Format("{0,-2}{1,-2}", row, board[row, col]));
                        }
                        else
                        {
                            Console.Write(String.Format("{0,-2}", board[row, col]));
                        }
                    }
                }
                Console.WriteLine();
            }

            void update(int[,] board)
            {
                Console.WriteLine("\n\nYour move - row [0..8] col [0..8] val [1-9] (ENTER to quit)");
                string move = Console.ReadLine();
                //Console.Write(move);
                int count = 0, row = 0, col = 0, val = 0;

                try
                {
                    for (int i = 0; i < move.Length; i++)
                    {
                        if (count == 0)
                        {
                            row = Convert.ToInt32(Convert.ToString(move[i]));
                        }

                        if (count == 2)
                        {
                            col = Convert.ToInt32(Convert.ToString(move[i]));
                        }

                        if (count == 4)
                        {
                            val = Convert.ToInt32(Convert.ToString(move[i]));
                        }

                        count++;
                    }
                    if (val == 0)
                    {
                        System.Environment.Exit(0);
                    }

                    if (checkMove(board, row, col, val) == true)
                    {
                        board[row, col] = val;
                        printBoard(board);
                        printAvailable();
                    }
                    else
                    {
                        Console.WriteLine("can't make the move");
                    }

                    update(bob);
                }catch (Exception o)
                { Console.WriteLine("Invalid input (format is {row col val})"); update(bob); }
            }

            string posAvailable(int[,] board, int row, int col) // print numbers available
            {
                int    i         = 1;
                string available = "";

                while (i < 10)
                {
                    if (board[row, col] > 0)
                    {
                        available = "..";
                    }
                    else
                    if (checkMove(board, row, col, i))
                    {
                        available += "" + i;
                    }
                    i++;
                }
                return(available);
            }

            void printAvailable()
            {
                Console.WriteLine();
                Console.Write(String.Format("{0,-6} {1,-6} {2,-6} {3,-6} {4,-6} {5,-6} {6,-6} {7,-6} {8,-6} {9,-6}\n", "", 0, 1, 2, 3, 4, 5, 6, 7, 8));
                for (int row = 0; row < 9; row++)
                {
                    Console.WriteLine();
                    for (int col = 0; col < 9; col++)
                    {
                        if (col == 0)
                        {
                            Console.Write(String.Format("{0,-6}", row));
                            Console.Write(String.Format("{0,-7}", posAvailable(bob, row, col)));
                        }
                        else
                        {
                            Console.Write(String.Format("{0,-7}", posAvailable(bob, row, col)));
                        }
                    }
                }
            }

            bool checkMove(int[,] board, int row, int col, int value) //if value doesnt exist in the row,col or box then true the move is valid
            {
                bool result = true;

                if (CheckRow(board, row, col, value) == true)
                {
                    return(false);
                }
                else if (CheckCol(board, row, col, value) == true)
                {
                    return(false);
                }
                else if (CheckBox(board, row, col, value) == true)
                {
                    return(false);
                }

                return(result);
            }

            bool CheckRow(int[,] board, int row, int col, int value) //if value exist in the row returns true
            {
                bool result = false;

                for (int i = 0; i < 9; i++)
                {
                    if (board[i, col] == value)
                    {
                        return(true);
                    }
                }

                return(result);
            }

            bool CheckCol(int[,] board, int row, int col, int value) //if value exist in the col returns true
            {
                bool result = false;

                for (int i = 0; i < 9; i++)
                {
                    if (board[row, i] == value)
                    {
                        return(true);
                    }
                }

                return(result);
            }

            bool CheckBox(int[,] board, int row, int col, int value)//if value exist in the box returns true
            {
                bool result   = false;
                int  startrow = 0;
                int  startcol = 0;
                int  c        = 0;

                if (row >= 0 && row <= 2)
                {
                    startrow = 0;
                }
                if (row >= 3 && row <= 5)
                {
                    startrow = 3;
                }
                if (row >= 6 && row <= 8)
                {
                    startrow = 6;
                }
                if (col >= 0 && row <= 2)
                {
                    startcol = 0;
                }
                if (col >= 3 && row <= 5)
                {
                    startcol = 3;
                }
                if (col >= 6 && row <= 8)
                {
                    startcol = 6;
                }
                for (int i = startrow; i < startrow + 3; i++)
                {
                    for (int j = startcol; j < startcol + 3; j++)
                    {
                        if (board[i, j] == value)
                        {
                            return(true);
                        }
                    }
                }

                return(result);
            }

            printBoard(bob);
            printAvailable();
            update(bob);
        } //Main
コード例 #6
0
        public static bool Assemble(string sourceName)
        {
            // Assembles source code from an input file sourceName and loads codeLen
            // words of code directly into memory PVM.mem[0 .. codeLen-1],
            // storing strings in the string pool at the top of memory in
            // PVM.mem[stkBase .. memSize-1].
            //
            // Returns true if assembly succeeded
            //
            // Instruction format :
            //    Instruction  = [ Label ] Opcode [ AddressField ] [ Comment ]
            //    Label        = [ "{" ] Integer [ "}" ]
            //    Opcode       = Mnemonic
            //    AddressField = Integer | "String"
            //    Comment      = String
            //
            // A string AddressField may only be used with a PRNS opcode
            // Instructions are supplied one to a line; terminated at end of input file

            string mnemonic; // mnemonic for matching
            char   quote;    // string delimiter

            src = new InFile(sourceName);
            if (src.OpenError())
            {
                Console.WriteLine("Could not open input file\n");
                System.Environment.Exit(1);
            }
            Console.WriteLine("Assembling code ... \n");
            codeLen = 0;
            stkBase = PVM.memSize - 1;
            okay    = true;
            do
            {
                SkipLabel();                         // ignore labels at start of line
                if (!src.EOF())                      // we must have a line
                {
                    mnemonic = ReadMnemonic();       // unpack mnemonic
                    if (mnemonic == null)
                    {
                        continue;                    // ignore directives
                    }
                    int op = PVM.OpCode(mnemonic);   // look it up
                    PVM.mem[codeLen] = op;           // store in memory
                    switch (op)
                    {
                    case PVM.prns:                   // requires a string address field
                        do
                        {
                            quote = src.ReadChar();  // search for opening quote character
                        }while (quote != '"' && quote != '\'' && quote != '\n');
                        codeLen          = (codeLen + 1) % PVM.memSize;
                        PVM.mem[codeLen] = stkBase - 1; // pointer to start of string
                        if (quote == '\n')
                        {
                            Error("Missing string address", codeLen);
                        }
                        else                         // stack string in literal pool
                        {
                            ch = src.ReadChar();
                            while (ch != quote && ch != '\n')
                            {
                                if (ch == '\\')
                                {
                                    ch = src.ReadChar();
                                    if (ch == '\n')  // closing quote missing
                                    {
                                        Error("Malformed string", codeLen);
                                    }
                                    switch (ch)
                                    {
                                    case 't': ch = '\t'; break;

                                    case 'n': ch = '\n'; break;

                                    case '\"': ch = '\"'; break;

                                    case '\'': ch = '\''; break;

                                    default: break;
                                    }
                                }
                                stkBase--;
                                PVM.mem[stkBase] = ch;
                                ch = src.ReadChar();
                            }
                            if (ch == '\n')          // closing quote missing
                            {
                                Error("Malformed string", codeLen);
                            }
                        }
                        stkBase--;
                        PVM.mem[stkBase] = 0;        // terminate string with nul
                        break;

                    case PVM.brn:                    // all require numeric address field
                    case PVM.bze:
                    case PVM.dsp:
                    case PVM.lda:
                    case PVM.ldc:
                        codeLen = (codeLen + 1) % PVM.memSize;
                        if (ch == '\n')              // no field could be found
                        {
                            Error("Missing address", codeLen);
                        }
                        else                         // unpack it and store
                        {
                            PVM.mem[codeLen] = src.ReadInt();
                            if (src.Error())
                            {
                                Error("Bad address", codeLen);
                            }
                        }
                        break;

                    case PVM.nul:                    // unrecognized mnemonic
                        Console.Write(mnemonic);
                        Error(" - Invalid opcode", codeLen);
                        break;

                    default:                         // no address needed
                        break;
                    }
                    if (!src.EOL())
                    {
                        src.ReadLn();                // skip comments
                    }
                    codeLen = (codeLen + 1) % PVM.memSize;
                }
            } while (!src.EOF());
            for (int i = codeLen; i < stkBase; i++)  // fill with invalid OpCodes
            {
                PVM.mem[i] = PVM.nul;
            }
            return(okay);
        }
コード例 #7
0
        public static bool Assemble(string sourceName)
        {
            string mnemonic; // mnemonic for matching
            char   quote;    // string delimiter

            src = new InFile(sourceName);
            if (src.OpenError())
            {
                Console.WriteLine("Could not open input file\n");
                System.Environment.Exit(1);
            }
            Console.WriteLine("Assembling code ... \n");
            codeLen = 0;
            stkBase = PVM.memSize - 1;
            okay    = true;
            do
            {
                SkipLabel();                         // ignore labels at start of line
                if (!src.EOF())                      // we must have a line
                {
                    mnemonic = ReadMnemonic();       // unpack mnemonic
                    if (mnemonic == null)
                    {
                        continue;                    // ignore directives
                    }
                    int op = PVM.OpCode(mnemonic);   // look it up
                    PVM.mem[codeLen] = op;           // store in memory
                    switch (op)
                    {
                    case PVM.prns:                   // requires a string address field
                        do
                        {
                            quote = src.ReadChar();  // search for opening quote character
                        }while (quote != '"' && quote != '\'' && quote != '\n');
                        codeLen          = (codeLen + 1) % PVM.memSize;
                        PVM.mem[codeLen] = stkBase - 1; // pointer to start of string
                        if (quote == '\n')
                        {
                            Error("Missing string address", codeLen);
                        }
                        else                         // stack string in literal pool
                        {
                            ch = src.ReadChar();
                            while (ch != quote && ch != '\n')
                            {
                                if (ch == '\\')
                                {
                                    ch = src.ReadChar();
                                    if (ch == '\n')  // closing quote missing
                                    {
                                        Error("Malformed string", codeLen);
                                    }
                                    switch (ch)
                                    {
                                    case 't': ch = '\t'; break;

                                    case 'n': ch = '\n'; break;

                                    case '\"': ch = '\"'; break;

                                    case '\'': ch = '\''; break;

                                    default: break;
                                    }
                                }
                                stkBase--;
                                PVM.mem[stkBase] = ch;
                                ch = src.ReadChar();
                            }
                            if (ch == '\n')          // closing quote missing
                            {
                                Error("Malformed string", codeLen);
                            }
                        }
                        stkBase--;
                        PVM.mem[stkBase] = 0;        // terminate string with nul
                        break;

                    case PVM.brn:                    // all require numeric address field
                    case PVM.bze:
                    case PVM.dsp:
                    case PVM.lda:
                    case PVM.ldc:
                    case PVM.ldl:
                    case PVM.stl:
                        codeLen = (codeLen + 1) % PVM.memSize;
                        if (ch == '\n')              // no field could be found
                        {
                            Error("Missing address", codeLen);
                        }
                        else                         // unpack it and store
                        {
                            PVM.mem[codeLen] = src.ReadInt();
                            if (src.Error())
                            {
                                Error("Bad address", codeLen);
                            }
                        }
                        break;

                    case PVM.nul:                    // unrecognized mnemonic
                        Console.Write(mnemonic);
                        Error(" - Invalid opcode", codeLen);
                        break;

                    default:                         // no address needed
                        break;
                    }
                    if (!src.EOL())
                    {
                        src.ReadLn();                // skip comments
                    }
                    codeLen = (codeLen + 1) % PVM.memSize;
                }
            } while (!src.EOF());
            for (int i = codeLen; i < stkBase; i++)  // fill with invalid OpCodes
            {
                PVM.mem[i] = PVM.nul;
            }
            return(okay);
        }
コード例 #8
0
        static bool suggestedBoardEmpty = false; //keep track of if the suggested board is empty
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("missing input file");
                System.Environment.Exit(1);
            }
            InFile data = new InFile(args[0]);

            if (data.OpenError())
            {
                Console.WriteLine("cannot open " + args[0]);
                System.Environment.Exit(1);
            }


            string [,] assignedBoard      = new string [9, 9];
            string [,] solutionBoard      = new string [9, 9];
            int    [,] predictionBoard    = new int [9, 9];
            int    [, ][,] suggestedBoard = new int [9, 9][, ];


            //read the already assinged board
            readBoard(assignedBoard, suggestedBoard, data, false);

            //read the solution board
            readBoard(solutionBoard, suggestedBoard, data, true);

            //update the suggestion board
            updateSuggestedBoard(assignedBoard, suggestedBoard);

            // game loop
            do
            {
                //create new suggestBoard
                Console.WriteLine("Still Assignable\n");
                printSuggestionBoard(suggestedBoard);

                Console.WriteLine("Already Assigned\n");
                //print the assigned board and predict the next assigned board
                printAndPredict(assignedBoard, suggestedBoard, predictionBoard);

                Console.WriteLine();
                Console.WriteLine(known + " squares known with " + predictions + " predictions");

                if (predictions != 0)
                {
                    Console.WriteLine("Hit any key to apply predictions");
                    Console.ReadLine();


                    applyPredictions(assignedBoard, suggestedBoard, predictionBoard);
                    predictions = 0; //reset the predictions
                }
                else
                {
                    Console.WriteLine("Your move - row[0..8] col[0..8] value[1..9] (0 to give up)?");
                    int   count  = 0;
                    int[] inputs = new int[3];
                    //get 3 numbers with a nice flow of input
                    while (count < 3)
                    {
                        string[] listInput = Console.ReadLine().Trim().Split(' ');
                        for (int i = 0; i < listInput.Length && count < 3; i++)
                        {
                            if (listInput[i].Trim().Length > 0)
                            {
                                inputs[count++] = Convert.ToInt32(listInput[i]);
                            }
                        }
                    }

                    int rowInput   = inputs[0];
                    int colInput   = inputs[1];
                    int valueInput = inputs[2];
                    //check for input validation
                    if (rowInput < 0 || rowInput > 8 || colInput < 0 || colInput > 8 || valueInput < 0 || valueInput > 9)
                    {
                        Console.WriteLine("******* Invalid");
                        continue;
                    }
                    //check if user gives up
                    if (rowInput == 0 && colInput == 0 && valueInput == 0)
                    {
                        Console.WriteLine("You gave up :(");
                        Console.WriteLine("\nHere is the solution: ");
                        printAndPredict(solutionBoard, suggestedBoard, predictionBoard);
                        System.Environment.Exit(0);
                    }

                    //check if the suggested block has the value the user entered
                    if (!getCurrentSet(suggestedBoard[rowInput, colInput]).Contains(valueInput))
                    {
                        Console.WriteLine("******* Invalid");
                        continue;
                    }
                    // The move is valid
                    assignedBoard[rowInput, colInput]  = " " + valueInput;
                    suggestedBoard[rowInput, colInput] = null; //block has now been filled get rid of suggestions
                    known++;
                }


                updateSuggestedBoard(assignedBoard, suggestedBoard);
            } while (known != 81 && !suggestedBoardEmpty);

            //print out finished boards
            Console.WriteLine("Still Assignable\n");
            printSuggestionBoard(suggestedBoard);

            Console.WriteLine("Already Assigned\n");
            printAndPredict(assignedBoard, suggestedBoard, predictionBoard); //also calucaltes predictions
            if (known != 81)                                                 //if the games ends without 81 well know blocks then the game can't be finished
            {
                Console.WriteLine("No more possible moves");
                Console.WriteLine();
                // Console.WriteLine("Here is the solution: ");
                //printAssignedBoard(solutionBoard, suggestedBoard, predictionBoard);
            }
            else
            {
                Console.WriteLine("Well done game complete!");
            }

            Console.ReadKey();
        }
コード例 #9
0
ファイル: prac1Temp.cs プロジェクト: TomMakkink/Sudoku.cs
    public static void Main(string[] args)
    {
        //List<string> temp = new List<string> { };
        //Screen MainScreen = new Screen();
        Screen.ClrScr();
        Screen.SetWindowSize(Console.WindowWidth, Console.WindowHeight);
        //Screen.DefaultColor();
        Screen.TextColor(0);
        Screen.BackgroundColor(3);
        InFile data = new InFile(args[0]);

        if (data.OpenError())
        {
            Console.WriteLine("cannot open " + args[0]);
            System.Environment.Exit(1);
        }
        int    i       = 0;
        int    tempInt = 9 * 9;
        IntSet mySet   = new IntSet(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List <IntSet>         mySets = new List <IntSet>();
        List <List <IntSet> > board  = new List <List <IntSet> >();

        //bool SolutionStarts = false;
        while (i < tempInt)
        {
            IntSet tempSet = new IntSet();
            int    temp    = data.ReadInt();
            tempSet.Incl(temp);
            if (temp != 0)
            {
                mySets.Add(tempSet);
            }
            if (temp == 0)
            {
                mySets.Add(mySet);
            }
            IO.Write(temp);
            i = i + 1;
            if (i != 0 && i % 9 == 0)
            {
                IO.Write('\n');
                board.Add(mySets);
                List <IntSet> tempSet2 = new List <IntSet>();
                mySets = tempSet2;
            }
        }
        IO.Write('\n');
        //IO.WriteLine(mySets.Count);
        //IO.WriteLine(board[1][0]);
        //IO.WriteLine(board[2][0]);
        IO.WriteLine("Please may you about to begin a new Game of Sudoku.");
        writeBoard();
        IO.WriteLine("Please may you you either type in (S)tart or (Q)uite");
        char T = IO.ReadChar();

        if (T == 'S' || T == 's')
        {
            bool GameNotFinished = true;
            while (GameNotFinished)
            {
                // Get input from user
                string [] userInput = getInputFromUser();
                while (!validateInput(userInput))
                {
                    IO.WriteLine("Invalid Input.");
                    userInput = getInputFromUser();
                }
                // List of valid input
                List <int> userMove = convertToIntList(userInput);
            }
        }
        if (T == 'q' || T == 'Q')
        {
            Screen.ClrScr(); System.Environment.Exit(1);
        }
    }