在软件开发中,跨平台兼容性是一个重要的考虑因素。这意味着应用程序可以在不同的操作系统上运行,而无需进行大量的修改。本文将介绍如何使用C++和Java创建可在Windows和Linux上运行的应用程序。
C++是一种强大的编程语言,它提供了跨平台开发的潜力。然而,使用Microsoft的Visual Studio IDE编写的程序默认只能在Windows上运行。为了使代码能够在Windows和Linux上成功执行,决定利用C++的可移植性。
在之前的文章中,详细记录了这种方法:。然而,这种方法受到了批评,因为“相同的代码”不能直接编译成平台应用程序。为了解决这个问题,将程序抽象成一系列函数,这些函数可以与原生GUI工具包接口。
在Windows上,仍然使用Visual Basic IDE来提供GUI。C++核心程序编译成托管的assembly DLL,提供函数。而在Linux上,使用Glade来开发GUI和相关代码,直接将函数添加到GUI代码中。
Sudoku游戏的图像使用未压缩的位图文件,在Windows BMP文件格式中。这种格式可以在Windows和Linux上显示,并且可以直接处理以生成程序所需的各种图像。
为了使Sudoku程序真正独立于平台,决定使用Java重写它。Java是一种解释型语言,它允许“一次编写,到处运行”,而不需要特别的准备。
使用Eclipse或NetBeans IDE,每个都有自己的GUI工具包。尽管使用Eclipse,但决定使用Java基础类(JFC)和Swing。代码可以用文本编辑器编写,使用javac编译,使用java运行应用程序。
以下是游戏运行所需的操作代码:
    public class Smethods {
        public static byte select(byte[][] sudoku, byte number, byte position, byte step) {
            if((sudoku[position*9 + number][step] == 0) || (sudoku[position*9 + number][step] > 9))
                return step;
            step += 1;
            int count = 0;
            for(count = 0; count < 729; count++)
                sudoku[count][step] = sudoku[count][step - 1];
            for(count = 0; count < 9; count++)
                sudoku[position*9 + count][step] = 0;
            byte row = (byte) (position/9);
            for(count = 0; count < 9; count++)
                sudoku[row * 81 + count * 9 + number][step] = 0;
            byte column = (byte) (position%9);
            for(count = 0; count < 9; count++)
                sudoku[column * 9 + count * 81 + number][step] = 0;
            int brow = (position/27)*243;
            column = (byte) (((position%9)/3)*27);
            byte incount;
            for(incount = 0; incount < 3; incount++) {
                for(count = 0; count < 3; count++)
                    sudoku[brow + column + count * 9 + incount * 81 + number ][step] = 0;
            }
            sudoku[position*9 + number][step] = (byte) (number + 11);
            return step;
        }
    }
    
以上代码只是Smethods类中的一个方法。还有:
    public static void start(byte[][] sudoku)
    public static void trysudoku(byte[][] sudoku, byte startstep)
    
这些提供了生成和解决Sudoku游戏所需的所有计算。
显示窗口由以下代码创建:
    public class MySudoku {
        public static byte[][] sudoku = new byte[729][82];
        public static byte step = 0;
        private static final int WindowWidth = 777;
        private static final int WindowHeight = 636;
        public static void ShowGUI() {
            Smethods.start(sudoku);
            final byte border = 14;
            JFrame f = new JFrame("MySudoku");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BufferedImage image = null;
            try {
                image = ImageIO.read(new File("sudoku.png"));
            } catch(IOException e) { }
            f.setResizable(false);
            f.setIconImage(image);
            f.setSize(WindowWidth, WindowHeight);
            f.setLocation(0, 0);
            f.setLayout(new BorderLayout());
            new SPanel(new Dimension(WindowWidth,border)), BorderLayout.NORTH);
            new SPanel(new Dimension(WindowWidth,border)), BorderLayout.SOUTH);
            new SPanel(new Dimension(border,WindowHeight)), BorderLayout.EAST);
            new SPanel(new Dimension(0,WindowHeight)), BorderLayout.WEST);
            DisplayPanel dp = new DisplayPanel();
            dp.setBackground(Color.BLACK);
            f.add(dp, BorderLayout.CENTER);
            f.setVisible(true);
        }
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    ShowGUI();
                }
            });
        }
    }
    
以上代码只是显示GUI的方法。
显示面板负责创建GUI的所有繁重工作。它定位和监控按钮,并绘制Sudoku游戏显示:
    public class DisplayPanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;
        private int DisplayWidth = 557;
        private int DisplayHeight = 580;
        private int ButtonsWidth = 200;
        private final Color LB = new Color(0xAD,0xD8, 0xE6);
        private final Color DB = new Color(0x1E,0x90, 0xFF);
        private final Color P = new Color(0x80, 0, 0x80);
        public DisplayPanel() {
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {            
                    selectNumber(e.getX(),e.getY());
                }
            });
            this.setLayout(new BorderLayout());
            JPanel pb = new JPanel();
            pb.setPreferredSize(new Dimension(ButtonsWidth,DisplayHeight));
            pb.setBackground(LB);
            FlowLayout FL = new FlowLayout();
            FL.setVgap(55);
            FL.setHgap(100);
            pb.setLayout(FL); 
            SButton EYS = new SButton("Enter Your Sudoku", "EYS");
            EYS.addActionListener(this);
            pb.add(EYS);
            SButton SHS = new SButton("Start Hard Sudoku", "SHS");
            SHS.addActionListener(this);
            pb.add(SHS);
            SButton SMS = new SButton("Start Medium Sudoku", "SMS");
            SMS.addActionListener(this);
            pb.add(SMS);
            SButton SES = new SButton("Start Easy Sudoku", "SES");
            SES.addActionListener(this);
            pb.add(SES);
            SButton GBS = new SButton("Go Back One Step", "GBS");
            GBS.addActionListener(this);
            pb.add(GBS);
            SButton STS = new SButton("Solve This Sudoku", "STS");
            STS.addActionListener(this);
            pb.add(STS);
            this.add(pb,BorderLayout.WEST);
        }
    }
    
以上代码只是构造函数。还有:
    private void selectNumber(int x, int y)
    public Dimension getPreferredSize()
    protected void paintComponent(Graphics g)
    public void actionPerformed(ActionEvent e)